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> |
Chris Craik | 98d608d | 2014-07-17 12:25:11 -0700 | [diff] [blame] | 24 | #include <SkColor.h> |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 25 | #include <SkShader.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 26 | #include <SkTypeface.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 27 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 28 | #include <utils/Log.h> |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 29 | #include <utils/StopWatch.h> |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 30 | |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 31 | #include <private/hwui/DrawGlInfo.h> |
| 32 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 33 | #include <ui/Rect.h> |
| 34 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 35 | #include "OpenGLRenderer.h" |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 36 | #include "DeferredDisplayList.h" |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 37 | #include "DisplayListRenderer.h" |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 38 | #include "Fence.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 39 | #include "GammaFontRenderer.h" |
| 40 | #include "Patch.h" |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 41 | #include "PathTessellator.h" |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 42 | #include "Properties.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 43 | #include "RenderNode.h" |
| 44 | #include "RenderState.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 45 | #include "ShadowTessellator.h" |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 46 | #include "SkiaShader.h" |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 47 | #include "Vector.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 48 | #include "VertexBuffer.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 49 | #include "utils/GLUtils.h" |
Chris Craik | 06e7fe5 | 2014-11-20 17:27:36 -0800 | [diff] [blame] | 50 | #include "utils/TraceUtils.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 51 | |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 52 | #if DEBUG_DETAILED_EVENTS |
| 53 | #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__) |
| 54 | #else |
| 55 | #define EVENT_LOGD(...) |
| 56 | #endif |
| 57 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 58 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 59 | namespace uirenderer { |
| 60 | |
Chris Craik | 67862524 | 2014-02-28 12:26:34 -0800 | [diff] [blame] | 61 | static GLenum getFilter(const SkPaint* paint) { |
| 62 | if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) { |
| 63 | return GL_LINEAR; |
| 64 | } |
| 65 | return GL_NEAREST; |
| 66 | } |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 67 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | // Globals |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Structure mapping Skia xfermodes to OpenGL blending factors. |
| 74 | */ |
| 75 | struct Blender { |
| 76 | SkXfermode::Mode mode; |
| 77 | GLenum src; |
| 78 | GLenum dst; |
| 79 | }; // struct Blender |
| 80 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 81 | // In this array, the index of each Blender equals the value of the first |
| 82 | // entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] |
| 83 | static const Blender gBlends[] = { |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 84 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 85 | { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, |
| 86 | { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, |
| 87 | { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 88 | { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 89 | { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 90 | { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 91 | { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 92 | { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 93 | { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 94 | { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 95 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 96 | { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, |
Derek Sollenberger | d81ec45 | 2013-02-04 15:42:26 -0500 | [diff] [blame] | 97 | { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR }, |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 98 | { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR } |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 99 | }; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 100 | |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 101 | // This array contains the swapped version of each SkXfermode. For instance |
| 102 | // this array's SrcOver blending mode is actually DstOver. You can refer to |
| 103 | // createLayer() for more information on the purpose of this array. |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 104 | static const Blender gBlendsSwap[] = { |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 105 | { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 106 | { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, |
| 107 | { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, |
| 108 | { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 109 | { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 110 | { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 111 | { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 112 | { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 113 | { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 114 | { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 115 | { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 116 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 117 | { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, |
Derek Sollenberger | d81ec45 | 2013-02-04 15:42:26 -0500 | [diff] [blame] | 118 | { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO }, |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 119 | { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE } |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 122 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 123 | // Functions |
| 124 | /////////////////////////////////////////////////////////////////////////////// |
| 125 | |
| 126 | template<typename T> |
| 127 | static inline T min(T a, T b) { |
| 128 | return a < b ? a : b; |
| 129 | } |
| 130 | |
| 131 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 132 | // Constructors/destructor |
| 133 | /////////////////////////////////////////////////////////////////////////////// |
| 134 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 135 | OpenGLRenderer::OpenGLRenderer(RenderState& renderState) |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 136 | : mState(*this) |
| 137 | , mFrameStarted(false) |
Chris Craik | 058fc64 | 2014-07-23 18:19:28 -0700 | [diff] [blame] | 138 | , mCaches(Caches::getInstance()) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 139 | , mExtensions(Extensions::getInstance()) |
Chris Craik | 058fc64 | 2014-07-23 18:19:28 -0700 | [diff] [blame] | 140 | , mRenderState(renderState) |
| 141 | , mScissorOptimizationDisabled(false) |
Chris Craik | 284b243 | 2014-09-18 16:05:35 -0700 | [diff] [blame] | 142 | , mSuppressTiling(false) |
| 143 | , mFirstFrameAfterResize(true) |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 144 | , mDirty(false) |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 145 | , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN}) |
Chris Craik | 058fc64 | 2014-07-23 18:19:28 -0700 | [diff] [blame] | 146 | , mLightRadius(FLT_MIN) |
| 147 | , mAmbientShadowAlpha(0) |
| 148 | , mSpotShadowAlpha(0) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 149 | // *set* draw modifiers to be 0 |
| 150 | memset(&mDrawModifiers, 0, sizeof(mDrawModifiers)); |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 151 | mDrawModifiers.mOverrideLayerAlpha = 1.0f; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 152 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 153 | memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 156 | OpenGLRenderer::~OpenGLRenderer() { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 157 | // The context has already been destroyed at this point, do not call |
| 158 | // GL APIs. All GL state should be kept in Caches.h |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 161 | void OpenGLRenderer::initProperties() { |
| 162 | char property[PROPERTY_VALUE_MAX]; |
| 163 | if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) { |
| 164 | mScissorOptimizationDisabled = !strcasecmp(property, "true"); |
| 165 | INIT_LOGD(" Scissor optimization %s", |
| 166 | mScissorOptimizationDisabled ? "disabled" : "enabled"); |
| 167 | } else { |
| 168 | INIT_LOGD(" Scissor optimization enabled"); |
| 169 | } |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Chris Craik | 058fc64 | 2014-07-23 18:19:28 -0700 | [diff] [blame] | 172 | void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius, |
| 173 | uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) { |
| 174 | mLightCenter = lightCenter; |
| 175 | mLightRadius = lightRadius; |
| 176 | mAmbientShadowAlpha = ambientShadowAlpha; |
| 177 | mSpotShadowAlpha = spotShadowAlpha; |
| 178 | } |
| 179 | |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 180 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 181 | // Setup |
| 182 | /////////////////////////////////////////////////////////////////////////////// |
| 183 | |
Chris Craik | 797b95b2 | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 184 | void OpenGLRenderer::onViewportInitialized() { |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 185 | glDisable(GL_DITHER); |
| 186 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 187 | |
| 188 | glEnableVertexAttribArray(Program::kBindingPosition); |
Chris Craik | 284b243 | 2014-09-18 16:05:35 -0700 | [diff] [blame] | 189 | mFirstFrameAfterResize = true; |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 192 | void OpenGLRenderer::setupFrameState(float left, float top, |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 193 | float right, float bottom, bool opaque) { |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 194 | mCaches.clearGarbage(); |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 195 | mState.initializeSaveStack(left, top, right, bottom, mLightCenter); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 196 | mOpaque = opaque; |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 197 | mTilingClip.set(left, top, right, bottom); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 200 | void OpenGLRenderer::startFrame() { |
| 201 | if (mFrameStarted) return; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 202 | mFrameStarted = true; |
| 203 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 204 | mState.setDirtyClip(true); |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 205 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 206 | discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 207 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 208 | mRenderState.setViewport(mState.getWidth(), mState.getHeight()); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 209 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 210 | // Functors break the tiling extension in pretty spectacular ways |
| 211 | // This ensures we don't use tiling when a functor is going to be |
| 212 | // invoked during the frame |
Chris Craik | 284b243 | 2014-09-18 16:05:35 -0700 | [diff] [blame] | 213 | mSuppressTiling = mCaches.hasRegisteredFunctors() |
| 214 | || mFirstFrameAfterResize; |
| 215 | mFirstFrameAfterResize = false; |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 216 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 217 | startTilingCurrentClip(true); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 218 | |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 219 | debugOverdraw(true, true); |
| 220 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 221 | clear(mTilingClip.left, mTilingClip.top, |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 222 | mTilingClip.right, mTilingClip.bottom, mOpaque); |
| 223 | } |
| 224 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 225 | void OpenGLRenderer::prepareDirty(float left, float top, |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 226 | float right, float bottom, bool opaque) { |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 227 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 228 | setupFrameState(left, top, right, bottom, opaque); |
| 229 | |
| 230 | // Layer renderers will start the frame immediately |
| 231 | // The framebuffer renderer will first defer the display list |
| 232 | // for each layer and wait until the first drawing command |
| 233 | // to start the frame |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 234 | if (currentSnapshot()->fbo == 0) { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 235 | syncState(); |
| 236 | updateLayers(); |
| 237 | } else { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 238 | startFrame(); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 239 | } |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Romain Guy | dcfc836 | 2013-01-03 13:08:57 -0800 | [diff] [blame] | 242 | void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) { |
| 243 | // If we know that we are going to redraw the entire framebuffer, |
| 244 | // perform a discard to let the driver know we don't need to preserve |
| 245 | // the back buffer for this frame. |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 246 | if (mExtensions.hasDiscardFramebuffer() && |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 247 | left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) { |
| 248 | const bool isFbo = onGetTargetFbo() == 0; |
Romain Guy | f158198 | 2013-01-31 17:20:30 -0800 | [diff] [blame] | 249 | const GLenum attachments[] = { |
| 250 | isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0, |
| 251 | isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT }; |
Romain Guy | dcfc836 | 2013-01-03 13:08:57 -0800 | [diff] [blame] | 252 | glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments); |
| 253 | } |
| 254 | } |
| 255 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 256 | void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 257 | if (!opaque) { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 258 | mCaches.enableScissor(); |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 259 | mCaches.setScissor(left, getViewportHeight() - bottom, right - left, bottom - top); |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 260 | glClear(GL_COLOR_BUFFER_BIT); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 261 | mDirty = true; |
| 262 | return; |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 263 | } |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 264 | |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 265 | mCaches.resetScissor(); |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void OpenGLRenderer::syncState() { |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 269 | if (mCaches.blend) { |
| 270 | glEnable(GL_BLEND); |
| 271 | } else { |
| 272 | glDisable(GL_BLEND); |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 273 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 274 | } |
| 275 | |
henry.uh_chen | 33f5a59 | 2014-07-02 19:36:56 +0800 | [diff] [blame] | 276 | void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) { |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 277 | if (!mSuppressTiling) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 278 | const Snapshot* snapshot = currentSnapshot(); |
| 279 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 280 | const Rect* clip = &mTilingClip; |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 281 | if (snapshot->flags & Snapshot::kFlagFboTarget) { |
| 282 | clip = &(snapshot->layer->clipRect); |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 283 | } |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 284 | |
henry.uh_chen | 33f5a59 | 2014-07-02 19:36:56 +0800 | [diff] [blame] | 285 | startTiling(*clip, getViewportHeight(), opaque, expand); |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
henry.uh_chen | 33f5a59 | 2014-07-02 19:36:56 +0800 | [diff] [blame] | 289 | void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) { |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 290 | if (!mSuppressTiling) { |
henry.uh_chen | 33f5a59 | 2014-07-02 19:36:56 +0800 | [diff] [blame] | 291 | if(expand) { |
| 292 | // Expand the startTiling region by 1 |
| 293 | int leftNotZero = (clip.left > 0) ? 1 : 0; |
| 294 | int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0; |
| 295 | |
| 296 | mCaches.startTiling( |
| 297 | clip.left - leftNotZero, |
| 298 | windowHeight - clip.bottom - topNotZero, |
| 299 | clip.right - clip.left + leftNotZero + 1, |
| 300 | clip.bottom - clip.top + topNotZero + 1, |
| 301 | opaque); |
| 302 | } else { |
| 303 | mCaches.startTiling(clip.left, windowHeight - clip.bottom, |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 304 | clip.right - clip.left, clip.bottom - clip.top, opaque); |
henry.uh_chen | 33f5a59 | 2014-07-02 19:36:56 +0800 | [diff] [blame] | 305 | } |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 306 | } |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void OpenGLRenderer::endTiling() { |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 310 | if (!mSuppressTiling) mCaches.endTiling(); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 313 | bool OpenGLRenderer::finish() { |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 314 | renderOverdraw(); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 315 | endTiling(); |
| 316 | |
Chris Craik | 4ac36f8 | 2014-12-09 16:54:03 -0800 | [diff] [blame] | 317 | for (size_t i = 0; i < mTempPaths.size(); i++) { |
| 318 | delete mTempPaths[i]; |
| 319 | } |
| 320 | mTempPaths.clear(); |
| 321 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 322 | // When finish() is invoked on FBO 0 we've reached the end |
| 323 | // of the current frame |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 324 | if (onGetTargetFbo() == 0) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 325 | mCaches.pathCache.trim(); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 326 | mCaches.tessellationCache.trim(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 329 | if (!suppressErrorChecks()) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 330 | #if DEBUG_OPENGL |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 331 | GLUtils::dumpGLErrors(); |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 332 | #endif |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 333 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 334 | #if DEBUG_MEMORY_USAGE |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 335 | mCaches.dumpMemoryUsage(); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 336 | #else |
| 337 | if (mCaches.getDebugLevel() & kDebugMemory) { |
| 338 | mCaches.dumpMemoryUsage(); |
| 339 | } |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 340 | #endif |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 341 | } |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 342 | |
| 343 | mFrameStarted = false; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 344 | |
| 345 | return reportAndClearDirty(); |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 348 | void OpenGLRenderer::resumeAfterLayer() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 349 | mRenderState.setViewport(getViewportWidth(), getViewportHeight()); |
| 350 | mRenderState.bindFramebuffer(currentSnapshot()->fbo); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 351 | debugOverdraw(true, false); |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 352 | |
| 353 | mCaches.resetScissor(); |
| 354 | dirtyClip(); |
| 355 | } |
| 356 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 357 | void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 358 | if (mState.currentlyIgnored()) return; |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 359 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 360 | Rect clip(*mState.currentClipRect()); |
Romain Guy | 80911b8 | 2011-03-16 15:30:12 -0700 | [diff] [blame] | 361 | clip.snapToPixelBoundaries(); |
| 362 | |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 363 | // Since we don't know what the functor will draw, let's dirty |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 364 | // the entire clip region |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 365 | if (hasLayer()) { |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 366 | dirtyLayerUnchecked(clip, getRegion()); |
| 367 | } |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 368 | |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 369 | DrawGlInfo info; |
| 370 | info.clipLeft = clip.left; |
| 371 | info.clipTop = clip.top; |
| 372 | info.clipRight = clip.right; |
| 373 | info.clipBottom = clip.bottom; |
| 374 | info.isLayer = hasLayer(); |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 375 | info.width = getViewportWidth(); |
| 376 | info.height = getViewportHeight(); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 377 | currentTransform()->copyTo(&info.transform[0]); |
Romain Guy | 80911b8 | 2011-03-16 15:30:12 -0700 | [diff] [blame] | 378 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 379 | bool prevDirtyClip = mState.getDirtyClip(); |
Chris Craik | 54f574a | 2013-08-26 11:23:46 -0700 | [diff] [blame] | 380 | // setup GL state for functor |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 381 | if (mState.getDirtyClip()) { |
Chris Craik | 54f574a | 2013-08-26 11:23:46 -0700 | [diff] [blame] | 382 | setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt() |
| 383 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 384 | if (mCaches.enableScissor() || prevDirtyClip) { |
John Reck | 25d2f7b | 2013-09-10 13:12:09 -0700 | [diff] [blame] | 385 | setScissorFromClip(); |
| 386 | } |
Chris Craik | 54f574a | 2013-08-26 11:23:46 -0700 | [diff] [blame] | 387 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 388 | mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info); |
| 389 | // Scissor may have been modified, reset dirty clip |
| 390 | dirtyClip(); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 391 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 392 | mDirty = true; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 395 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 396 | // Debug |
| 397 | /////////////////////////////////////////////////////////////////////////////// |
| 398 | |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 399 | void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const { |
| 400 | #if DEBUG_DETAILED_EVENTS |
| 401 | const int BUFFER_SIZE = 256; |
| 402 | va_list ap; |
| 403 | char buf[BUFFER_SIZE]; |
| 404 | |
| 405 | va_start(ap, fmt); |
| 406 | vsnprintf(buf, BUFFER_SIZE, fmt, ap); |
| 407 | va_end(ap); |
| 408 | |
| 409 | eventMark(buf); |
| 410 | #endif |
| 411 | } |
| 412 | |
| 413 | |
Romain Guy | 0f667533 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 414 | void OpenGLRenderer::eventMark(const char* name) const { |
| 415 | mCaches.eventMark(0, name); |
| 416 | } |
| 417 | |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 418 | void OpenGLRenderer::startMark(const char* name) const { |
| 419 | mCaches.startMark(0, name); |
| 420 | } |
| 421 | |
| 422 | void OpenGLRenderer::endMark() const { |
| 423 | mCaches.endMark(); |
| 424 | } |
| 425 | |
| 426 | void OpenGLRenderer::debugOverdraw(bool enable, bool clear) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 427 | mRenderState.debugOverdraw(enable, clear); |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void OpenGLRenderer::renderOverdraw() { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 431 | if (mCaches.debugOverdraw && onGetTargetFbo() == 0) { |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 432 | const Rect* clip = &mTilingClip; |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 433 | |
| 434 | mCaches.enableScissor(); |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 435 | mCaches.setScissor(clip->left, mState.firstSnapshot()->getViewportHeight() - clip->bottom, |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 436 | clip->right - clip->left, clip->bottom - clip->top); |
| 437 | |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 438 | // 1x overdraw |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 439 | mCaches.stencil.enableDebugTest(2); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 440 | drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode); |
| 441 | |
| 442 | // 2x overdraw |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 443 | mCaches.stencil.enableDebugTest(3); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 444 | drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode); |
| 445 | |
| 446 | // 3x overdraw |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 447 | mCaches.stencil.enableDebugTest(4); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 448 | drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode); |
| 449 | |
| 450 | // 4x overdraw and higher |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 451 | mCaches.stencil.enableDebugTest(4, true); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 452 | drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode); |
| 453 | |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 454 | mCaches.stencil.disable(); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 459 | // Layers |
| 460 | /////////////////////////////////////////////////////////////////////////////// |
| 461 | |
| 462 | bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) { |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 463 | if (layer->deferredUpdateScheduled && layer->renderer |
| 464 | && layer->renderNode.get() && layer->renderNode->isRenderable()) { |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 465 | |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 466 | if (inFrame) { |
| 467 | endTiling(); |
| 468 | debugOverdraw(false, false); |
| 469 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 470 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 471 | if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) { |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 472 | layer->render(*this); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 473 | } else { |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 474 | layer->defer(*this); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 475 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 476 | |
| 477 | if (inFrame) { |
| 478 | resumeAfterLayer(); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 479 | startTilingCurrentClip(); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 482 | layer->debugDrawUpdate = mCaches.debugLayersUpdates; |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 483 | layer->hasDrawnSinceUpdate = false; |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 484 | |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | void OpenGLRenderer::updateLayers() { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 492 | // If draw deferring is enabled this method will simply defer |
| 493 | // the display list of each individual layer. The layers remain |
| 494 | // in the layer updates list which will be cleared by flushLayers(). |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 495 | int count = mLayerUpdates.size(); |
| 496 | if (count > 0) { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 497 | if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { |
| 498 | startMark("Layer Updates"); |
| 499 | } else { |
| 500 | startMark("Defer Layer Updates"); |
| 501 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 502 | |
Chris Craik | 1206b9b | 2013-04-04 14:46:24 -0700 | [diff] [blame] | 503 | // Note: it is very important to update the layers in order |
| 504 | for (int i = 0; i < count; i++) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 505 | Layer* layer = mLayerUpdates.itemAt(i).get(); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 506 | updateLayer(layer, false); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 507 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 508 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 509 | if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { |
| 510 | mLayerUpdates.clear(); |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 511 | mRenderState.bindFramebuffer(onGetTargetFbo()); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 512 | } |
| 513 | endMark(); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void OpenGLRenderer::flushLayers() { |
| 518 | int count = mLayerUpdates.size(); |
| 519 | if (count > 0) { |
| 520 | startMark("Apply Layer Updates"); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 521 | |
Chris Craik | 1206b9b | 2013-04-04 14:46:24 -0700 | [diff] [blame] | 522 | // Note: it is very important to update the layers in order |
| 523 | for (int i = 0; i < count; i++) { |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 524 | mLayerUpdates.itemAt(i)->flush(); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | mLayerUpdates.clear(); |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 528 | mRenderState.bindFramebuffer(onGetTargetFbo()); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 529 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 530 | endMark(); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | void OpenGLRenderer::pushLayerUpdate(Layer* layer) { |
| 535 | if (layer) { |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 536 | // Make sure we don't introduce duplicates. |
| 537 | // SortedVector would do this automatically but we need to respect |
| 538 | // the insertion order. The linear search is not an issue since |
| 539 | // this list is usually very short (typically one item, at most a few) |
| 540 | for (int i = mLayerUpdates.size() - 1; i >= 0; i--) { |
| 541 | if (mLayerUpdates.itemAt(i) == layer) { |
| 542 | return; |
| 543 | } |
| 544 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 545 | mLayerUpdates.push_back(layer); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 549 | void OpenGLRenderer::cancelLayerUpdate(Layer* layer) { |
| 550 | if (layer) { |
| 551 | for (int i = mLayerUpdates.size() - 1; i >= 0; i--) { |
| 552 | if (mLayerUpdates.itemAt(i) == layer) { |
| 553 | mLayerUpdates.removeAt(i); |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 554 | break; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 560 | void OpenGLRenderer::flushLayerUpdates() { |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 561 | ATRACE_NAME("Update HW Layers"); |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 562 | syncState(); |
| 563 | updateLayers(); |
| 564 | flushLayers(); |
| 565 | // Wait for all the layer updates to be executed |
| 566 | AutoFence fence; |
| 567 | } |
| 568 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 569 | void OpenGLRenderer::markLayersAsBuildLayers() { |
| 570 | for (size_t i = 0; i < mLayerUpdates.size(); i++) { |
| 571 | mLayerUpdates[i]->wasBuildLayered = true; |
| 572 | } |
| 573 | } |
| 574 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 575 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 576 | // State management |
| 577 | /////////////////////////////////////////////////////////////////////////////// |
| 578 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 579 | void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) { |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 580 | bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 581 | bool restoreClip = removed.flags & Snapshot::kFlagClipSet; |
| 582 | bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 583 | |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 584 | if (restoreViewport) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 585 | mRenderState.setViewport(getViewportWidth(), getViewportHeight()); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 588 | if (restoreClip) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 589 | dirtyClip(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 590 | } |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 591 | |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 592 | if (restoreLayer) { |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 593 | endMark(); // Savelayer |
Chris Craik | a8bea8e | 2014-09-24 11:29:43 -0700 | [diff] [blame] | 594 | ATRACE_END(); // SaveLayer |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 595 | startMark("ComposeLayer"); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 596 | composeLayer(removed, restored); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 597 | endMark(); |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 598 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 601 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 602 | // Layers |
| 603 | /////////////////////////////////////////////////////////////////////////////// |
| 604 | |
| 605 | int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 606 | const SkPaint* paint, int flags, const SkPath* convexMask) { |
Chris Craik | 4ace730 | 2014-09-14 15:49:54 -0700 | [diff] [blame] | 607 | // force matrix/clip isolation for layer |
| 608 | flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag; |
| 609 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 610 | const int count = mState.saveSnapshot(flags); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 611 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 612 | if (!mState.currentlyIgnored()) { |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 613 | createLayer(left, top, right, bottom, paint, flags, convexMask); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 614 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 615 | |
| 616 | return count; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 619 | void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) { |
| 620 | const Rect untransformedBounds(bounds); |
| 621 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 622 | currentTransform()->mapRect(bounds); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 623 | |
| 624 | // Layers only make sense if they are in the framebuffer's bounds |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 625 | if (bounds.intersect(*mState.currentClipRect())) { |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 626 | // We cannot work with sub-pixels in this case |
| 627 | bounds.snapToPixelBoundaries(); |
| 628 | |
| 629 | // When the layer is not an FBO, we may use glCopyTexImage so we |
| 630 | // need to make sure the layer does not extend outside the bounds |
| 631 | // of the framebuffer |
Chris Craik | 9241975 | 2014-05-15 13:21:28 -0700 | [diff] [blame] | 632 | const Snapshot& previous = *(currentSnapshot()->previous); |
| 633 | Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight()); |
| 634 | if (!bounds.intersect(previousViewport)) { |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 635 | bounds.setEmpty(); |
| 636 | } else if (fboLayer) { |
| 637 | clip.set(bounds); |
| 638 | mat4 inverse; |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 639 | inverse.loadInverse(*currentTransform()); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 640 | inverse.mapRect(clip); |
| 641 | clip.snapToPixelBoundaries(); |
| 642 | if (clip.intersect(untransformedBounds)) { |
| 643 | clip.translate(-untransformedBounds.left, -untransformedBounds.top); |
| 644 | bounds.set(untransformedBounds); |
| 645 | } else { |
| 646 | clip.setEmpty(); |
| 647 | } |
| 648 | } |
| 649 | } else { |
| 650 | bounds.setEmpty(); |
| 651 | } |
| 652 | } |
| 653 | |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 654 | void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip, |
| 655 | bool fboLayer, int alpha) { |
| 656 | if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || |
| 657 | bounds.getHeight() > mCaches.maxTextureSize || |
| 658 | (fboLayer && clip.isEmpty())) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 659 | writableSnapshot()->empty = fboLayer; |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 660 | } else { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 661 | writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer); |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 665 | int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom, |
Derek Sollenberger | d44fbe5 | 2014-02-05 16:47:00 -0500 | [diff] [blame] | 666 | const SkPaint* paint, int flags) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 667 | const int count = mState.saveSnapshot(flags); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 668 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 669 | if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) { |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 670 | // initialize the snapshot as though it almost represents an FBO layer so deferred draw |
| 671 | // operations will be able to store and restore the current clip and transform info, and |
| 672 | // quick rejection will be correct (for display lists) |
| 673 | |
| 674 | Rect bounds(left, top, right, bottom); |
| 675 | Rect clip; |
| 676 | calculateLayerBoundsAndClip(bounds, clip, true); |
Derek Sollenberger | d44fbe5 | 2014-02-05 16:47:00 -0500 | [diff] [blame] | 677 | updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint)); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 678 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 679 | if (!mState.currentlyIgnored()) { |
| 680 | writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f); |
| 681 | writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom); |
| 682 | writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight()); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 683 | writableSnapshot()->roundRectClipState = nullptr; |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | return count; |
| 688 | } |
| 689 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 690 | /** |
| 691 | * Layers are viewed by Skia are slightly different than layers in image editing |
| 692 | * programs (for instance.) When a layer is created, previously created layers |
| 693 | * and the frame buffer still receive every drawing command. For instance, if a |
| 694 | * layer is created and a shape intersecting the bounds of the layers and the |
| 695 | * framebuffer is draw, the shape will be drawn on both (unless the layer was |
| 696 | * created with the SkCanvas::kClipToLayer_SaveFlag flag.) |
| 697 | * |
| 698 | * A way to implement layers is to create an FBO for each layer, backed by an RGBA |
| 699 | * texture. Unfortunately, this is inefficient as it requires every primitive to |
| 700 | * be drawn n + 1 times, where n is the number of active layers. In practice this |
| 701 | * means, for every primitive: |
| 702 | * - Switch active frame buffer |
| 703 | * - Change viewport, clip and projection matrix |
| 704 | * - Issue the drawing |
| 705 | * |
| 706 | * Switching rendering target n + 1 times per drawn primitive is extremely costly. |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 707 | * To avoid this, layers are implemented in a different way here, at least in the |
| 708 | * general case. FBOs are used, as an optimization, when the "clip to layer" flag |
| 709 | * is set. When this flag is set we can redirect all drawing operations into a |
| 710 | * single FBO. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 711 | * |
| 712 | * This implementation relies on the frame buffer being at least RGBA 8888. When |
| 713 | * a layer is created, only a texture is created, not an FBO. The content of the |
| 714 | * 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] | 715 | * 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] | 716 | * buffer and drawing continues as normal. This technique therefore treats the |
| 717 | * frame buffer as a scratch buffer for the layers. |
| 718 | * |
| 719 | * To compose the layers back onto the frame buffer, each layer texture |
| 720 | * (containing the original frame buffer data) is drawn as a simple quad over |
| 721 | * the frame buffer. The trick is that the quad is set as the composition |
| 722 | * destination in the blending equation, and the frame buffer becomes the source |
| 723 | * of the composition. |
| 724 | * |
| 725 | * Drawing layers with an alpha value requires an extra step before composition. |
| 726 | * An empty quad is drawn over the layer's region in the frame buffer. This quad |
| 727 | * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the |
| 728 | * quad is used to multiply the colors in the frame buffer. This is achieved by |
| 729 | * changing the GL blend functions for the GL_FUNC_ADD blend equation to |
| 730 | * GL_ZERO, GL_SRC_ALPHA. |
| 731 | * |
| 732 | * Because glCopyTexImage2D() can be slow, an alternative implementation might |
| 733 | * be use to draw a single clipped layer. The implementation described above |
| 734 | * is correct in every case. |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 735 | * |
| 736 | * (1) The frame buffer is actually not cleared right away. To allow the GPU |
| 737 | * to potentially optimize series of calls to glCopyTexImage2D, the frame |
| 738 | * buffer is left untouched until the first drawing operation. Only when |
| 739 | * something actually gets drawn are the layers regions cleared. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 740 | */ |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 741 | bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom, |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 742 | const SkPaint* paint, int flags, const SkPath* convexMask) { |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 743 | LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); |
| 744 | LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 745 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 746 | const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; |
| 747 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 748 | // Window coordinates of the layer |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 749 | Rect clip; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 750 | Rect bounds(left, top, right, bottom); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 751 | calculateLayerBoundsAndClip(bounds, clip, fboLayer); |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 752 | updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint)); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 753 | |
| 754 | // Bail out if we won't draw in this snapshot |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 755 | if (mState.currentlyIgnored()) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 756 | return false; |
| 757 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 758 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 759 | mCaches.activeTexture(0); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 760 | Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight()); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 761 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 762 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 765 | layer->setPaint(paint); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 766 | layer->layer.set(bounds); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 767 | layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()), |
| 768 | bounds.getWidth() / float(layer->getWidth()), 0.0f); |
Derek Sollenberger | d44fbe5 | 2014-02-05 16:47:00 -0500 | [diff] [blame] | 769 | |
Chet Haase | a23eed8 | 2012-04-12 15:19:04 -0700 | [diff] [blame] | 770 | layer->setBlend(true); |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 771 | layer->setDirty(false); |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 772 | layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 773 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 774 | // Save the layer in the snapshot |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 775 | writableSnapshot()->flags |= Snapshot::kFlagIsLayer; |
| 776 | writableSnapshot()->layer = layer; |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 777 | |
Chris Craik | a8bea8e | 2014-09-24 11:29:43 -0700 | [diff] [blame] | 778 | ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u", |
| 779 | fboLayer ? "" : "unclipped ", |
| 780 | layer->getWidth(), layer->getHeight()); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 781 | startMark("SaveLayer"); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 782 | if (fboLayer) { |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 783 | return createFboLayer(layer, bounds, clip); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 784 | } else { |
| 785 | // Copy the framebuffer into the layer |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 786 | layer->bindTexture(); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 787 | if (!bounds.isEmpty()) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 788 | if (layer->isEmpty()) { |
Romain Guy | b254c24 | 2013-06-27 17:15:24 -0700 | [diff] [blame] | 789 | // Workaround for some GL drivers. When reading pixels lying outside |
| 790 | // of the window we should get undefined values for those pixels. |
| 791 | // Unfortunately some drivers will turn the entire target texture black |
| 792 | // when reading outside of the window. |
| 793 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(), |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 794 | 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 795 | layer->setEmpty(false); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 796 | } |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 797 | |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 798 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, |
| 799 | bounds.left, getViewportHeight() - bounds.bottom, |
| 800 | bounds.getWidth(), bounds.getHeight()); |
Romain Guy | b254c24 | 2013-06-27 17:15:24 -0700 | [diff] [blame] | 801 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 802 | // Enqueue the buffer coordinates to clear the corresponding region later |
| 803 | mLayers.push(new Rect(bounds)); |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 804 | } |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 805 | } |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 806 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 807 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 810 | bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) { |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 811 | layer->clipRect.set(clip); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 812 | layer->setFbo(mCaches.fboCache.get()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 813 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 814 | writableSnapshot()->region = &writableSnapshot()->layer->region; |
| 815 | writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer; |
| 816 | writableSnapshot()->fbo = layer->getFbo(); |
| 817 | writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f); |
| 818 | writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom); |
| 819 | writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight()); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 820 | writableSnapshot()->roundRectClipState = nullptr; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 821 | |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 822 | endTiling(); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 823 | debugOverdraw(false, false); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 824 | // Bind texture to FBO |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 825 | mRenderState.bindFramebuffer(layer->getFbo()); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 826 | layer->bindTexture(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 827 | |
| 828 | // Initialize the texture if needed |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 829 | if (layer->isEmpty()) { |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 830 | layer->allocateTexture(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 831 | layer->setEmpty(false); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 835 | layer->getTexture(), 0); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 836 | |
henry.uh_chen | 33f5a59 | 2014-07-02 19:36:56 +0800 | [diff] [blame] | 837 | // Expand the startTiling region by 1 |
| 838 | startTilingCurrentClip(true, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 839 | |
| 840 | // 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] | 841 | mCaches.enableScissor(); |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 842 | mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 843 | clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 844 | glClear(GL_COLOR_BUFFER_BIT); |
| 845 | |
| 846 | dirtyClip(); |
| 847 | |
| 848 | // Change the ortho projection |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 849 | mRenderState.setViewport(bounds.getWidth(), bounds.getHeight()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 850 | return true; |
| 851 | } |
| 852 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 853 | /** |
| 854 | * Read the documentation of createLayer() before doing anything in this method. |
| 855 | */ |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 856 | void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) { |
| 857 | if (!removed.layer) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 858 | ALOGE("Attempting to compose a layer that does not exist"); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 859 | return; |
| 860 | } |
| 861 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 862 | Layer* layer = removed.layer; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 863 | const Rect& rect = layer->layer; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 864 | const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 865 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 866 | bool clipRequired = false; |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 867 | mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom, |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 868 | &clipRequired, nullptr, false); // safely ignore return, should never be rejected |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 869 | mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired); |
| 870 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 871 | if (fboLayer) { |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 872 | endTiling(); |
| 873 | |
Romain Guy | e0aa84b | 2012-04-03 19:30:26 -0700 | [diff] [blame] | 874 | // Detach the texture from the FBO |
| 875 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 876 | |
| 877 | layer->removeFbo(false); |
| 878 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 879 | // Unbind current FBO and restore previous one |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 880 | mRenderState.bindFramebuffer(restored.fbo); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 881 | debugOverdraw(true, false); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 882 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 883 | startTilingCurrentClip(); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 886 | if (!fboLayer && layer->getAlpha() < 255) { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 887 | SkPaint layerPaint; |
| 888 | layerPaint.setAlpha(layer->getAlpha()); |
| 889 | layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode); |
| 890 | layerPaint.setColorFilter(layer->getColorFilter()); |
| 891 | |
| 892 | drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 893 | // Required below, composeLayerRect() will divide by 255 |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 894 | layer->setAlpha(255); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 897 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 898 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 899 | mCaches.activeTexture(0); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 900 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 901 | // When the layer is stored in an FBO, we can save a bit of fillrate by |
| 902 | // drawing only the dirty region |
| 903 | if (fboLayer) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 904 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 905 | composeLayerRegion(layer, rect); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 906 | } else if (!rect.isEmpty()) { |
| 907 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); |
Digish Pandya | a5ff739 | 2013-11-04 06:30:25 +0530 | [diff] [blame] | 908 | |
| 909 | save(0); |
| 910 | // the layer contains screen buffer content that shouldn't be alpha modulated |
| 911 | // (and any necessary alpha modulation was handled drawing into the layer) |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 912 | writableSnapshot()->alpha = 1.0f; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 913 | composeLayerRect(layer, rect, true); |
Digish Pandya | a5ff739 | 2013-11-04 06:30:25 +0530 | [diff] [blame] | 914 | restore(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 915 | } |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 916 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 917 | dirtyClip(); |
| 918 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 919 | // Failing to add the layer to the cache should happen only if the layer is too large |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 920 | layer->setConvexMask(nullptr); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 921 | if (!mCaches.layerCache.put(layer)) { |
Chris Craik | 07adacf | 2014-12-19 10:08:40 -0800 | [diff] [blame] | 922 | LAYER_LOGD("Deleting layer"); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 923 | layer->decStrong(nullptr); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 927 | void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) { |
Romain Guy | 2458939 | 2013-06-19 12:17:01 -0700 | [diff] [blame] | 928 | float alpha = getLayerAlpha(layer); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 929 | |
| 930 | setupDraw(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 931 | if (layer->getRenderTarget() == GL_TEXTURE_2D) { |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 932 | setupDrawWithTexture(); |
| 933 | } else { |
| 934 | setupDrawWithExternalTexture(); |
| 935 | } |
| 936 | setupDrawTextureTransform(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 937 | setupDrawColor(alpha, alpha, alpha, alpha); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 938 | setupDrawColorFilter(layer->getColorFilter()); |
| 939 | setupDrawBlending(layer); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 940 | setupDrawProgram(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 941 | setupDrawPureColorUniforms(); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 942 | setupDrawColorFilterUniforms(layer->getColorFilter()); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 943 | if (layer->getRenderTarget() == GL_TEXTURE_2D) { |
| 944 | setupDrawTexture(layer->getTexture()); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 945 | } else { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 946 | setupDrawExternalTexture(layer->getTexture()); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 947 | } |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 948 | if (currentTransform()->isPureTranslate() && |
Chris Craik | 9757ac0 | 2014-02-25 18:50:17 -0800 | [diff] [blame] | 949 | !layer->getForceFilter() && |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 950 | layer->getWidth() == (uint32_t) rect.getWidth() && |
| 951 | layer->getHeight() == (uint32_t) rect.getHeight()) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 952 | const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f); |
| 953 | const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 954 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 955 | layer->setFilter(GL_NEAREST); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 956 | setupDrawModelView(kModelViewMode_TranslateAndScale, false, |
| 957 | x, y, x + rect.getWidth(), y + rect.getHeight(), true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 958 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 959 | layer->setFilter(GL_LINEAR); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 960 | setupDrawModelView(kModelViewMode_TranslateAndScale, false, |
| 961 | rect.left, rect.top, rect.right, rect.bottom); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 962 | } |
| 963 | setupDrawTextureTransformUniforms(layer->getTexTransform()); |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 964 | setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 965 | |
| 966 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 969 | void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 970 | if (layer->isTextureLayer()) { |
| 971 | EVENT_LOGD("composeTextureLayerRect"); |
| 972 | resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); |
| 973 | drawTextureLayer(layer, rect); |
| 974 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 975 | } else { |
| 976 | EVENT_LOGD("composeHardwareLayerRect"); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 977 | const Rect& texCoords = layer->texCoords; |
| 978 | resetDrawTextureTexCoords(texCoords.left, texCoords.top, |
| 979 | texCoords.right, texCoords.bottom); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 980 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 981 | float x = rect.left; |
| 982 | float y = rect.top; |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 983 | bool simpleTransform = currentTransform()->isPureTranslate() && |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 984 | layer->getWidth() == (uint32_t) rect.getWidth() && |
Romain Guy | b247915 | 2011-07-08 11:57:29 -0700 | [diff] [blame] | 985 | layer->getHeight() == (uint32_t) rect.getHeight(); |
| 986 | |
| 987 | if (simpleTransform) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 988 | // When we're swapping, the layer is already in screen coordinates |
| 989 | if (!swap) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 990 | x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f); |
| 991 | y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 994 | layer->setFilter(GL_NEAREST, true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 995 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 996 | layer->setFilter(GL_LINEAR, true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 999 | SkPaint layerPaint; |
| 1000 | layerPaint.setAlpha(getLayerAlpha(layer) * 255); |
| 1001 | layerPaint.setXfermodeMode(layer->getMode()); |
| 1002 | layerPaint.setColorFilter(layer->getColorFilter()); |
| 1003 | |
| 1004 | bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1005 | drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(), |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1006 | layer->getTexture(), &layerPaint, blend, |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 1007 | &mMeshVertices[0].x, &mMeshVertices[0].u, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1008 | GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1009 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1010 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1011 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 1014 | /** |
| 1015 | * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated |
| 1016 | * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw |
| 1017 | * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used |
| 1018 | * by saveLayer's restore |
| 1019 | */ |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1020 | #define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \ |
| 1021 | DRAW_COMMAND; \ |
| 1022 | if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \ |
| 1023 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \ |
| 1024 | DRAW_COMMAND; \ |
| 1025 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \ |
| 1026 | } \ |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | #define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND) |
| 1030 | |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1031 | // This class is purely for inspection. It inherits from SkShader, but Skia does not know how to |
| 1032 | // use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque. |
| 1033 | class LayerShader : public SkShader { |
| 1034 | public: |
| 1035 | LayerShader(Layer* layer, const SkMatrix* localMatrix) |
| 1036 | : INHERITED(localMatrix) |
| 1037 | , mLayer(layer) { |
| 1038 | } |
| 1039 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1040 | virtual bool asACustomShader(void** data) const override { |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1041 | if (data) { |
| 1042 | *data = static_cast<void*>(mLayer); |
| 1043 | } |
| 1044 | return true; |
| 1045 | } |
| 1046 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1047 | virtual bool isOpaque() const override { |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1048 | return !mLayer->isBlend(); |
| 1049 | } |
| 1050 | |
| 1051 | protected: |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 1052 | virtual void shadeSpan(int x, int y, SkPMColor[], int count) { |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1053 | LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend."); |
| 1054 | } |
| 1055 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1056 | virtual void flatten(SkWriteBuffer&) const override { |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1057 | LOG_ALWAYS_FATAL("LayerShader should never be flattened."); |
| 1058 | } |
| 1059 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1060 | virtual Factory getFactory() const override { |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1061 | LOG_ALWAYS_FATAL("LayerShader should never be created from a stream."); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1062 | return nullptr; |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1063 | } |
| 1064 | private: |
| 1065 | // Unowned. |
| 1066 | Layer* mLayer; |
| 1067 | typedef SkShader INHERITED; |
| 1068 | }; |
| 1069 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1070 | void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1071 | if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw |
| 1072 | |
| 1073 | if (layer->getConvexMask()) { |
| 1074 | save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag); |
| 1075 | |
| 1076 | // clip to the area of the layer the mask can be larger |
| 1077 | clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op); |
| 1078 | |
| 1079 | SkPaint paint; |
| 1080 | paint.setAntiAlias(true); |
| 1081 | paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0)); |
| 1082 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1083 | // create LayerShader to map SaveLayer content into subsequent draw |
| 1084 | SkMatrix shaderMatrix; |
| 1085 | shaderMatrix.setTranslate(rect.left, rect.bottom); |
| 1086 | shaderMatrix.preScale(1, -1); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1087 | LayerShader layerShader(layer, &shaderMatrix); |
| 1088 | paint.setShader(&layerShader); |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1089 | |
| 1090 | // Since the drawing primitive is defined in local drawing space, |
| 1091 | // we don't need to modify the draw matrix |
| 1092 | const SkPath* maskPath = layer->getConvexMask(); |
| 1093 | DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint)); |
| 1094 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1095 | paint.setShader(nullptr); |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1096 | restore(); |
| 1097 | |
| 1098 | return; |
| 1099 | } |
| 1100 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1101 | if (layer->region.isRect()) { |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 1102 | layer->setRegionAsRect(); |
| 1103 | |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 1104 | DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect)); |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 1105 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1106 | layer->region.clear(); |
| 1107 | return; |
| 1108 | } |
| 1109 | |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 1110 | EVENT_LOGD("composeLayerRegion"); |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1111 | // standard Region based draw |
| 1112 | size_t count; |
| 1113 | const android::Rect* rects; |
| 1114 | Region safeRegion; |
| 1115 | if (CC_LIKELY(hasRectToRectTransform())) { |
| 1116 | rects = layer->region.getArray(&count); |
| 1117 | } else { |
| 1118 | safeRegion = Region::createTJunctionFreeRegion(layer->region); |
| 1119 | rects = safeRegion.getArray(&count); |
| 1120 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1121 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1122 | const float alpha = getLayerAlpha(layer); |
| 1123 | const float texX = 1.0f / float(layer->getWidth()); |
| 1124 | const float texY = 1.0f / float(layer->getHeight()); |
| 1125 | const float height = rect.getHeight(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1126 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1127 | setupDraw(); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1128 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1129 | // We must get (and therefore bind) the region mesh buffer |
| 1130 | // after we setup drawing in case we need to mess with the |
| 1131 | // stencil buffer in setupDraw() |
| 1132 | TextureVertex* mesh = mCaches.getRegionMesh(); |
| 1133 | uint32_t numQuads = 0; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1134 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1135 | setupDrawWithTexture(); |
| 1136 | setupDrawColor(alpha, alpha, alpha, alpha); |
| 1137 | setupDrawColorFilter(layer->getColorFilter()); |
| 1138 | setupDrawBlending(layer); |
| 1139 | setupDrawProgram(); |
| 1140 | setupDrawDirtyRegionsDisabled(); |
| 1141 | setupDrawPureColorUniforms(); |
| 1142 | setupDrawColorFilterUniforms(layer->getColorFilter()); |
| 1143 | setupDrawTexture(layer->getTexture()); |
| 1144 | if (currentTransform()->isPureTranslate()) { |
| 1145 | const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f); |
| 1146 | const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1147 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1148 | layer->setFilter(GL_NEAREST); |
| 1149 | setupDrawModelView(kModelViewMode_Translate, false, |
| 1150 | x, y, x + rect.getWidth(), y + rect.getHeight(), true); |
| 1151 | } else { |
| 1152 | layer->setFilter(GL_LINEAR); |
| 1153 | setupDrawModelView(kModelViewMode_Translate, false, |
| 1154 | rect.left, rect.top, rect.right, rect.bottom); |
| 1155 | } |
| 1156 | setupDrawMeshIndices(&mesh[0].x, &mesh[0].u); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1157 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1158 | for (size_t i = 0; i < count; i++) { |
| 1159 | const android::Rect* r = &rects[i]; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1160 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1161 | const float u1 = r->left * texX; |
| 1162 | const float v1 = (height - r->top) * texY; |
| 1163 | const float u2 = r->right * texX; |
| 1164 | const float v2 = (height - r->bottom) * texY; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1165 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1166 | // TODO: Reject quads outside of the clip |
| 1167 | TextureVertex::set(mesh++, r->left, r->top, u1, v1); |
| 1168 | TextureVertex::set(mesh++, r->right, r->top, u2, v1); |
| 1169 | TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); |
| 1170 | TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1171 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1172 | numQuads++; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1173 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1174 | if (numQuads >= gMaxNumberOfQuads) { |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 1175 | DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6, |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1176 | GL_UNSIGNED_SHORT, nullptr)); |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1177 | numQuads = 0; |
| 1178 | mesh = mCaches.getRegionMesh(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1179 | } |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | if (numQuads > 0) { |
| 1183 | DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6, |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1184 | GL_UNSIGNED_SHORT, nullptr)); |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1185 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1186 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1187 | #if DEBUG_LAYERS_AS_REGIONS |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1188 | drawRegionRectsDebug(layer->region); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1189 | #endif |
| 1190 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 1191 | layer->region.clear(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1194 | #if DEBUG_LAYERS_AS_REGIONS |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 1195 | void OpenGLRenderer::drawRegionRectsDebug(const Region& region) { |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1196 | size_t count; |
| 1197 | const android::Rect* rects = region.getArray(&count); |
| 1198 | |
| 1199 | uint32_t colors[] = { |
| 1200 | 0x7fff0000, 0x7f00ff00, |
| 1201 | 0x7f0000ff, 0x7fff00ff, |
| 1202 | }; |
| 1203 | |
| 1204 | int offset = 0; |
| 1205 | int32_t top = rects[0].top; |
| 1206 | |
| 1207 | for (size_t i = 0; i < count; i++) { |
| 1208 | if (top != rects[i].top) { |
| 1209 | offset ^= 0x2; |
| 1210 | top = rects[i].top; |
| 1211 | } |
| 1212 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1213 | SkPaint paint; |
| 1214 | paint.setColor(colors[offset + (i & 0x1)]); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1215 | Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1216 | drawColorRect(r.left, r.top, r.right, r.bottom, paint); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1217 | } |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1218 | } |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 1219 | #endif |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1220 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1221 | void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1222 | Vector<float> rects; |
| 1223 | |
| 1224 | SkRegion::Iterator it(region); |
| 1225 | while (!it.done()) { |
| 1226 | const SkIRect& r = it.rect(); |
| 1227 | rects.push(r.fLeft); |
| 1228 | rects.push(r.fTop); |
| 1229 | rects.push(r.fRight); |
| 1230 | rects.push(r.fBottom); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1231 | it.next(); |
| 1232 | } |
| 1233 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1234 | drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1237 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 1238 | const float right, const float bottom, const mat4 transform) { |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1239 | if (hasLayer()) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1240 | Rect bounds(left, top, right, bottom); |
| 1241 | transform.mapRect(bounds); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1242 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1243 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 1247 | const float right, const float bottom) { |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1248 | if (hasLayer()) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1249 | Rect bounds(left, top, right, bottom); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1250 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 1251 | } |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1255 | if (bounds.intersect(*mState.currentClipRect())) { |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 1256 | bounds.snapToPixelBoundaries(); |
| 1257 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 1258 | if (!dirty.isEmpty()) { |
| 1259 | region->orSelf(dirty); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1260 | } |
| 1261 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 1264 | void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) { |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 1265 | GLsizei elementsCount = quadsCount * 6; |
| 1266 | while (elementsCount > 0) { |
| 1267 | GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6); |
| 1268 | |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 1269 | setupDrawIndexedVertices(&mesh[0].x); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1270 | glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr); |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 1271 | |
| 1272 | elementsCount -= drawCount; |
| 1273 | // Though there are 4 vertices in a quad, we use 6 indices per |
| 1274 | // quad to draw with GL_TRIANGLES |
| 1275 | mesh += (drawCount / 6) * 4; |
| 1276 | } |
| 1277 | } |
| 1278 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1279 | void OpenGLRenderer::clearLayerRegions() { |
| 1280 | const size_t count = mLayers.size(); |
| 1281 | if (count == 0) return; |
| 1282 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1283 | if (!mState.currentlyIgnored()) { |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 1284 | EVENT_LOGD("clearLayerRegions"); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1285 | // Doing several glScissor/glClear here can negatively impact |
| 1286 | // GPUs with a tiler architecture, instead we draw quads with |
| 1287 | // the Clear blending mode |
| 1288 | |
| 1289 | // The list contains bounds that have already been clipped |
| 1290 | // against their initial clip rect, and the current clip |
| 1291 | // is likely different so we need to disable clipping here |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1292 | bool scissorChanged = mCaches.disableScissor(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1293 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 1294 | Vertex mesh[count * 4]; |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1295 | Vertex* vertex = mesh; |
| 1296 | |
| 1297 | for (uint32_t i = 0; i < count; i++) { |
| 1298 | Rect* bounds = mLayers.itemAt(i); |
| 1299 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1300 | Vertex::set(vertex++, bounds->left, bounds->top); |
| 1301 | Vertex::set(vertex++, bounds->right, bounds->top); |
| 1302 | Vertex::set(vertex++, bounds->left, bounds->bottom); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1303 | Vertex::set(vertex++, bounds->right, bounds->bottom); |
| 1304 | |
| 1305 | delete bounds; |
| 1306 | } |
Romain Guy | e67307c | 2013-02-11 18:01:20 -0800 | [diff] [blame] | 1307 | // We must clear the list of dirty rects before we |
| 1308 | // call setupDraw() to prevent stencil setup to do |
| 1309 | // the same thing again |
| 1310 | mLayers.clear(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1311 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1312 | SkPaint clearPaint; |
| 1313 | clearPaint.setXfermodeMode(SkXfermode::kClear_Mode); |
| 1314 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1315 | setupDraw(false); |
| 1316 | setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1317 | setupDrawBlending(&clearPaint, true); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1318 | setupDrawProgram(); |
| 1319 | setupDrawPureColorUniforms(); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 1320 | setupDrawModelView(kModelViewMode_Translate, false, |
| 1321 | 0.0f, 0.0f, 0.0f, 0.0f, true); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1322 | |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 1323 | issueIndexedQuadDraw(&mesh[0], count); |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1324 | |
| 1325 | if (scissorChanged) mCaches.enableScissor(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1326 | } else { |
| 1327 | for (uint32_t i = 0; i < count; i++) { |
| 1328 | delete mLayers.itemAt(i); |
| 1329 | } |
Romain Guy | e67307c | 2013-02-11 18:01:20 -0800 | [diff] [blame] | 1330 | mLayers.clear(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1331 | } |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1334 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1335 | // State Deferral |
| 1336 | /////////////////////////////////////////////////////////////////////////////// |
| 1337 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1338 | bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1339 | const Rect* currentClip = mState.currentClipRect(); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1340 | const mat4* currentMatrix = currentTransform(); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1341 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1342 | if (stateDeferFlags & kStateDeferFlag_Draw) { |
| 1343 | // state has bounds initialized in local coordinates |
| 1344 | if (!state.mBounds.isEmpty()) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1345 | currentMatrix->mapRect(state.mBounds); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1346 | Rect clippedBounds(state.mBounds); |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 1347 | // NOTE: if we ever want to use this clipping info to drive whether the scissor |
| 1348 | // is used, it should more closely duplicate the quickReject logic (in how it uses |
| 1349 | // snapToPixelBoundaries) |
| 1350 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1351 | if(!clippedBounds.intersect(*currentClip)) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1352 | // quick rejected |
| 1353 | return true; |
| 1354 | } |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1355 | |
Chris Craik | a02c4ed | 2013-06-14 13:43:58 -0700 | [diff] [blame] | 1356 | state.mClipSideFlags = kClipSide_None; |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1357 | if (!currentClip->contains(state.mBounds)) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1358 | int& flags = state.mClipSideFlags; |
| 1359 | // op partially clipped, so record which sides are clipped for clip-aware merging |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1360 | if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left; |
| 1361 | if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top; |
| 1362 | if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right; |
| 1363 | if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom; |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1364 | } |
| 1365 | state.mBounds.set(clippedBounds); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1366 | } else { |
Chris Craik | d72b73c | 2013-06-17 13:52:06 -0700 | [diff] [blame] | 1367 | // Empty bounds implies size unknown. Label op as conservatively clipped to disable |
| 1368 | // overdraw avoidance (since we don't know what it overlaps) |
| 1369 | state.mClipSideFlags = kClipSide_ConservativeFull; |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1370 | state.mBounds.set(*currentClip); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1374 | state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip); |
| 1375 | if (state.mClipValid) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1376 | state.mClip.set(*currentClip); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 1379 | // Transform, drawModifiers, and alpha always deferred, since they are used by state operations |
| 1380 | // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything) |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1381 | state.mMatrix.load(*currentMatrix); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 1382 | state.mDrawModifiers = mDrawModifiers; |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1383 | state.mAlpha = currentSnapshot()->alpha; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1384 | |
| 1385 | // always store/restore, since it's just a pointer |
| 1386 | state.mRoundRectClipState = currentSnapshot()->roundRectClipState; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1387 | return false; |
| 1388 | } |
| 1389 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1390 | void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 1391 | setMatrix(state.mMatrix); |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1392 | writableSnapshot()->alpha = state.mAlpha; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 1393 | mDrawModifiers = state.mDrawModifiers; |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1394 | writableSnapshot()->roundRectClipState = state.mRoundRectClipState; |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1395 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1396 | if (state.mClipValid && !skipClipRestore) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1397 | writableSnapshot()->setClip(state.mClip.left, state.mClip.top, |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1398 | state.mClip.right, state.mClip.bottom); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1399 | dirtyClip(); |
| 1400 | } |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1403 | /** |
| 1404 | * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done |
| 1405 | * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at |
| 1406 | * least one op is clipped), or disabled entirely (because no merged op is clipped) |
| 1407 | * |
| 1408 | * This method should be called when restoreDisplayState() won't be restoring the clip |
| 1409 | */ |
| 1410 | void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1411 | if (clipRect != nullptr) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1412 | writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1413 | } else { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1414 | writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight()); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1415 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1416 | dirtyClip(); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1417 | mCaches.setScissorEnabled(clipRect != nullptr || mScissorOptimizationDisabled); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1420 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1421 | // Clipping |
| 1422 | /////////////////////////////////////////////////////////////////////////////// |
| 1423 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 1424 | void OpenGLRenderer::setScissorFromClip() { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1425 | Rect clip(*mState.currentClipRect()); |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 1426 | clip.snapToPixelBoundaries(); |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 1427 | |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 1428 | if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom, |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1429 | clip.getWidth(), clip.getHeight())) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1430 | mState.setDirtyClip(false); |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1431 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1434 | void OpenGLRenderer::ensureStencilBuffer() { |
| 1435 | // Thanks to the mismatch between EGL and OpenGL ES FBO we |
| 1436 | // cannot attach a stencil buffer to fbo0 dynamically. Let's |
| 1437 | // just hope we have one when hasLayer() returns false. |
| 1438 | if (hasLayer()) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1439 | attachStencilBufferToLayer(currentSnapshot()->layer); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) { |
| 1444 | // The layer's FBO is already bound when we reach this stage |
| 1445 | if (!layer->getStencilRenderBuffer()) { |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 1446 | // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer |
| 1447 | // is attached after we initiated tiling. We must turn it off, |
| 1448 | // attach the new render buffer then turn tiling back on |
| 1449 | endTiling(); |
| 1450 | |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 1451 | RenderBuffer* buffer = mCaches.renderBufferCache.get( |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 1452 | Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight()); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1453 | layer->setStencilRenderBuffer(buffer); |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 1454 | |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 1455 | startTiling(layer->clipRect, layer->layer.getHeight()); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | void OpenGLRenderer::setStencilFromClip() { |
| 1460 | if (!mCaches.debugOverdraw) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1461 | if (!currentSnapshot()->clipRegion->isEmpty()) { |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 1462 | EVENT_LOGD("setStencilFromClip - enabling"); |
| 1463 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1464 | // NOTE: The order here is important, we must set dirtyClip to false |
| 1465 | // before any draw call to avoid calling back into this method |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1466 | mState.setDirtyClip(false); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1467 | |
| 1468 | ensureStencilBuffer(); |
| 1469 | |
| 1470 | mCaches.stencil.enableWrite(); |
| 1471 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1472 | // Clear and update the stencil, but first make sure we restrict drawing |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1473 | // to the region's bounds |
| 1474 | bool resetScissor = mCaches.enableScissor(); |
| 1475 | if (resetScissor) { |
| 1476 | // The scissor was not set so we now need to update it |
| 1477 | setScissorFromClip(); |
| 1478 | } |
| 1479 | mCaches.stencil.clear(); |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1480 | |
| 1481 | // stash and disable the outline clip state, since stencil doesn't account for outline |
| 1482 | bool storedSkipOutlineClip = mSkipOutlineClip; |
| 1483 | mSkipOutlineClip = true; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1484 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1485 | SkPaint paint; |
Chris Craik | 98d608d | 2014-07-17 12:25:11 -0700 | [diff] [blame] | 1486 | paint.setColor(SK_ColorBLACK); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1487 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 1488 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1489 | // NOTE: We could use the region contour path to generate a smaller mesh |
| 1490 | // Since we are using the stencil we could use the red book path |
| 1491 | // drawing technique. It might increase bandwidth usage though. |
| 1492 | |
| 1493 | // The last parameter is important: we are not drawing in the color buffer |
| 1494 | // so we don't want to dirty the current layer, if any |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1495 | drawRegionRects(*(currentSnapshot()->clipRegion), paint, false); |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1496 | if (resetScissor) mCaches.disableScissor(); |
| 1497 | mSkipOutlineClip = storedSkipOutlineClip; |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1498 | |
| 1499 | mCaches.stencil.enableTest(); |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 1500 | |
| 1501 | // Draw the region used to generate the stencil if the appropriate debug |
| 1502 | // mode is enabled |
| 1503 | if (mCaches.debugStencilClip == Caches::kStencilShowRegion) { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1504 | paint.setColor(0x7f0000ff); |
| 1505 | paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
| 1506 | drawRegionRects(*(currentSnapshot()->clipRegion), paint); |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 1507 | } |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1508 | } else { |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 1509 | EVENT_LOGD("setStencilFromClip - disabling"); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1510 | mCaches.stencil.disable(); |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 1515 | /** |
| 1516 | * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out. |
| 1517 | * |
| 1518 | * @param paint if not null, the bounds will be expanded to account for stroke depending on paint |
| 1519 | * style, and tessellated AA ramp |
| 1520 | */ |
| 1521 | bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 1522 | const SkPaint* paint) { |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 1523 | bool snapOut = paint && paint->isAntiAlias(); |
| 1524 | |
| 1525 | if (paint && paint->getStyle() != SkPaint::kFill_Style) { |
| 1526 | float outset = paint->getStrokeWidth() * 0.5f; |
| 1527 | left -= outset; |
| 1528 | top -= outset; |
| 1529 | right += outset; |
| 1530 | bottom += outset; |
| 1531 | } |
| 1532 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1533 | bool clipRequired = false; |
| 1534 | bool roundRectClipRequired = false; |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1535 | if (mState.calculateQuickRejectForScissor(left, top, right, bottom, |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1536 | &clipRequired, &roundRectClipRequired, snapOut)) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 1537 | return true; |
| 1538 | } |
| 1539 | |
Chris Craik | f23b25a | 2014-06-26 15:46:20 -0700 | [diff] [blame] | 1540 | // not quick rejected, so enable the scissor if clipRequired |
| 1541 | mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired); |
| 1542 | mSkipOutlineClip = !roundRectClipRequired; |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 1543 | return false; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1546 | void OpenGLRenderer::debugClip() { |
| 1547 | #if DEBUG_CLIP_REGIONS |
Chris Craik | f23b25a | 2014-06-26 15:46:20 -0700 | [diff] [blame] | 1548 | if (!currentSnapshot()->clipRegion->isEmpty()) { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1549 | SkPaint paint; |
| 1550 | paint.setColor(0x7f00ff00); |
| 1551 | drawRegionRects(*(currentSnapshot()->clipRegion, paint); |
| 1552 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1553 | } |
| 1554 | #endif |
| 1555 | } |
| 1556 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1557 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1558 | // Drawing commands |
| 1559 | /////////////////////////////////////////////////////////////////////////////// |
| 1560 | |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 1561 | void OpenGLRenderer::setupDraw(bool clearLayer) { |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 1562 | // TODO: It would be best if we could do this before quickRejectSetupScissor() |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1563 | // changes the scissor test state |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 1564 | if (clearLayer) clearLayerRegions(); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1565 | // Make sure setScissor & setStencil happen at the beginning of |
| 1566 | // this method |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1567 | if (mState.getDirtyClip()) { |
Chris Craik | b98a016 | 2013-02-21 11:30:22 -0800 | [diff] [blame] | 1568 | if (mCaches.scissorEnabled) { |
| 1569 | setScissorFromClip(); |
| 1570 | } |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 1571 | |
Dohyun Lee | adc0d9d | 2014-11-24 21:08:15 +0900 | [diff] [blame] | 1572 | setStencilFromClip(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1573 | } |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 1574 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1575 | mDescription.reset(); |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 1576 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1577 | mSetShaderColor = false; |
| 1578 | mColorSet = false; |
| 1579 | mColorA = mColorR = mColorG = mColorB = 0.0f; |
| 1580 | mTextureUnit = 0; |
| 1581 | mTrackDirtyRegions = true; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 1582 | |
| 1583 | // Enable debug highlight when what we're about to draw is tested against |
| 1584 | // the stencil buffer and if stencil highlight debugging is on |
| 1585 | mDescription.hasDebugHighlight = !mCaches.debugOverdraw && |
| 1586 | mCaches.debugStencilClip == Caches::kStencilShowHighlight && |
| 1587 | mCaches.stencil.isTestEnabled(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { |
| 1591 | mDescription.hasTexture = true; |
| 1592 | mDescription.hasAlpha8Texture = isAlpha8; |
| 1593 | } |
| 1594 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 1595 | void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) { |
| 1596 | mDescription.hasTexture = true; |
| 1597 | mDescription.hasColors = true; |
| 1598 | mDescription.hasAlpha8Texture = isAlpha8; |
| 1599 | } |
| 1600 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1601 | void OpenGLRenderer::setupDrawWithExternalTexture() { |
| 1602 | mDescription.hasExternalTexture = true; |
| 1603 | } |
| 1604 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1605 | void OpenGLRenderer::setupDrawNoTexture() { |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 1606 | mCaches.disableTexCoordsVertexArray(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 1609 | void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) { |
| 1610 | mDescription.hasVertexAlpha = true; |
| 1611 | mDescription.useShadowAlphaInterp = useShadowAlphaInterp; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1614 | void OpenGLRenderer::setupDrawColor(int color, int alpha) { |
| 1615 | mColorA = alpha / 255.0f; |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 1616 | mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f; |
| 1617 | mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f; |
| 1618 | mColorB = mColorA * ((color ) & 0xFF) / 255.0f; |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1619 | mColorSet = true; |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 1620 | mSetShaderColor = mDescription.setColorModulate(mColorA); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1623 | void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { |
| 1624 | mColorA = alpha / 255.0f; |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 1625 | mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f; |
| 1626 | mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f; |
| 1627 | mColorB = mColorA * ((color ) & 0xFF) / 255.0f; |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1628 | mColorSet = true; |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 1629 | mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 1632 | void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) { |
| 1633 | mCaches.fontRenderer->describe(mDescription, paint); |
| 1634 | } |
| 1635 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1636 | void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { |
| 1637 | mColorA = a; |
| 1638 | mColorR = r; |
| 1639 | mColorG = g; |
| 1640 | mColorB = b; |
| 1641 | mColorSet = true; |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 1642 | mSetShaderColor = mDescription.setColorModulate(a); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1643 | } |
| 1644 | |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1645 | void OpenGLRenderer::setupDrawShader(const SkShader* shader) { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1646 | if (shader != nullptr) { |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1647 | SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1648 | } |
| 1649 | } |
| 1650 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1651 | void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1652 | if (filter == nullptr) { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1653 | return; |
| 1654 | } |
| 1655 | |
| 1656 | SkXfermode::Mode mode; |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1657 | if (filter->asColorMode(nullptr, &mode)) { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1658 | mDescription.colorOp = ProgramDescription::kColorBlend; |
| 1659 | mDescription.colorMode = mode; |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1660 | } else if (filter->asColorMatrix(nullptr)) { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1661 | mDescription.colorOp = ProgramDescription::kColorMatrix; |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1662 | } |
| 1663 | } |
| 1664 | |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1665 | void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) { |
| 1666 | if (mColorSet && mode == SkXfermode::kClear_Mode) { |
| 1667 | mColorA = 1.0f; |
| 1668 | mColorR = mColorG = mColorB = 0.0f; |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1669 | mSetShaderColor = mDescription.modulate = true; |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1673 | void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) { |
| 1674 | SkXfermode::Mode mode = layer->getMode(); |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1675 | // When the blending mode is kClear_Mode, we need to use a modulate color |
| 1676 | // argb=1,0,0,0 |
| 1677 | accountForClear(mode); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1678 | // TODO: check shader blending, once we have shader drawing support for layers. |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1679 | bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f || |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1680 | (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter()); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1681 | chooseBlending(blend, mode, mDescription, swapSrcDst); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1684 | void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) { |
| 1685 | SkXfermode::Mode mode = getXfermodeDirect(paint); |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1686 | // When the blending mode is kClear_Mode, we need to use a modulate color |
| 1687 | // argb=1,0,0,0 |
| 1688 | accountForClear(mode); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1689 | blend |= (mColorSet && mColorA < 1.0f) || |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1690 | (getShader(paint) && !getShader(paint)->isOpaque()) || |
| 1691 | isBlendedColorFilter(getColorFilter(paint)); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1692 | chooseBlending(blend, mode, mDescription, swapSrcDst); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | void OpenGLRenderer::setupDrawProgram() { |
| 1696 | useProgram(mCaches.programCache.get(mDescription)); |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1697 | if (mDescription.hasRoundRectClip) { |
| 1698 | // TODO: avoid doing this repeatedly, stashing state pointer in program |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 1699 | const RoundRectClipState* state = writableSnapshot()->roundRectClipState; |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 1700 | const Rect& innerRect = state->innerRect; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1701 | glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"), |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 1702 | innerRect.left, innerRect.top, |
| 1703 | innerRect.right, innerRect.bottom); |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1704 | glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"), |
| 1705 | 1, GL_FALSE, &state->matrix.data[0]); |
Chris Craik | 4340c26 | 2014-09-11 18:58:45 -0700 | [diff] [blame] | 1706 | |
| 1707 | // add half pixel to round out integer rect space to cover pixel centers |
| 1708 | float roundedOutRadius = state->radius + 0.5f; |
| 1709 | glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"), |
| 1710 | roundedOutRadius); |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 1711 | } |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { |
| 1715 | mTrackDirtyRegions = false; |
| 1716 | } |
| 1717 | |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 1718 | void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset, |
| 1719 | float left, float top, float right, float bottom, bool ignoreTransform) { |
Chris Craik | e10e827 | 2014-05-08 14:28:26 -0700 | [diff] [blame] | 1720 | mModelViewMatrix.loadTranslate(left, top, 0.0f); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 1721 | if (mode == kModelViewMode_TranslateAndScale) { |
Chris Craik | e10e827 | 2014-05-08 14:28:26 -0700 | [diff] [blame] | 1722 | mModelViewMatrix.scale(right - left, bottom - top, 1.0f); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1723 | } |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 1724 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1725 | bool dirty = right - left > 0.0f && bottom - top > 0.0f; |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 1726 | const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform(); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1727 | mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(), |
| 1728 | mModelViewMatrix, transformMatrix, offset); |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 1729 | if (dirty && mTrackDirtyRegions) { |
| 1730 | if (!ignoreTransform) { |
| 1731 | dirtyLayer(left, top, right, bottom, *currentTransform()); |
| 1732 | } else { |
| 1733 | dirtyLayer(left, top, right, bottom); |
| 1734 | } |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1735 | } |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1736 | } |
| 1737 | |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1738 | void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) { |
| 1739 | if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1740 | mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); |
| 1741 | } |
| 1742 | } |
| 1743 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1744 | void OpenGLRenderer::setupDrawPureColorUniforms() { |
Romain Guy | 5536841 | 2010-12-14 10:59:41 -0800 | [diff] [blame] | 1745 | if (mSetShaderColor) { |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1746 | mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); |
Romain Guy | 5536841 | 2010-12-14 10:59:41 -0800 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1750 | void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1751 | if (shader == nullptr) { |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1752 | return; |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1753 | } |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 1754 | |
| 1755 | if (ignoreTransform) { |
| 1756 | // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform() |
| 1757 | // because it was built into modelView / the geometry, and the description needs to |
| 1758 | // compensate. |
| 1759 | mat4 modelViewWithoutTransform; |
| 1760 | modelViewWithoutTransform.loadInverse(*currentTransform()); |
| 1761 | modelViewWithoutTransform.multiply(mModelViewMatrix); |
| 1762 | mModelViewMatrix.load(modelViewWithoutTransform); |
| 1763 | } |
| 1764 | |
| 1765 | SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1766 | } |
| 1767 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1768 | void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1769 | if (nullptr == filter) { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1770 | return; |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1771 | } |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1772 | |
| 1773 | SkColor color; |
| 1774 | SkXfermode::Mode mode; |
| 1775 | if (filter->asColorMode(&color, &mode)) { |
| 1776 | const int alpha = SkColorGetA(color); |
| 1777 | const GLfloat a = alpha / 255.0f; |
| 1778 | const GLfloat r = a * SkColorGetR(color) / 255.0f; |
| 1779 | const GLfloat g = a * SkColorGetG(color) / 255.0f; |
| 1780 | const GLfloat b = a * SkColorGetB(color) / 255.0f; |
| 1781 | glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a); |
| 1782 | return; |
| 1783 | } |
| 1784 | |
| 1785 | SkScalar srcColorMatrix[20]; |
| 1786 | if (filter->asColorMatrix(srcColorMatrix)) { |
| 1787 | |
| 1788 | float colorMatrix[16]; |
| 1789 | memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float)); |
| 1790 | memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float)); |
| 1791 | memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float)); |
| 1792 | memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float)); |
| 1793 | |
| 1794 | // Skia uses the range [0..255] for the addition vector, but we need |
| 1795 | // the [0..1] range to apply the vector in GLSL |
| 1796 | float colorVector[4]; |
| 1797 | colorVector[0] = srcColorMatrix[4] / 255.0f; |
| 1798 | colorVector[1] = srcColorMatrix[9] / 255.0f; |
| 1799 | colorVector[2] = srcColorMatrix[14] / 255.0f; |
| 1800 | colorVector[3] = srcColorMatrix[19] / 255.0f; |
| 1801 | |
| 1802 | glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1, |
| 1803 | GL_FALSE, colorMatrix); |
| 1804 | glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector); |
| 1805 | return; |
| 1806 | } |
| 1807 | |
| 1808 | // it is an error if we ever get here |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 1811 | void OpenGLRenderer::setupDrawTextGammaUniforms() { |
| 1812 | mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram); |
| 1813 | } |
| 1814 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1815 | void OpenGLRenderer::setupDrawSimpleMesh() { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1816 | bool force = mCaches.bindMeshBuffer(); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1817 | mCaches.bindPositionVertexPointer(force, nullptr); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1818 | mCaches.unbindIndicesBuffer(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | void OpenGLRenderer::setupDrawTexture(GLuint texture) { |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 1822 | if (texture) bindTexture(texture); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 1823 | mTextureUnit++; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1824 | mCaches.enableTexCoordsVertexArray(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1825 | } |
| 1826 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1827 | void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { |
| 1828 | bindExternalTexture(texture); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 1829 | mTextureUnit++; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1830 | mCaches.enableTexCoordsVertexArray(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 1833 | void OpenGLRenderer::setupDrawTextureTransform() { |
| 1834 | mDescription.hasTextureTransform = true; |
| 1835 | } |
| 1836 | |
| 1837 | void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1838 | glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, |
| 1839 | GL_FALSE, &transform.data[0]); |
| 1840 | } |
| 1841 | |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 1842 | void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices, |
| 1843 | const GLvoid* texCoords, GLuint vbo) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1844 | bool force = false; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1845 | if (!vertices || vbo) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1846 | force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1847 | } else { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1848 | force = mCaches.unbindMeshBuffer(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1849 | } |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 1850 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1851 | mCaches.bindPositionVertexPointer(force, vertices); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1852 | if (mCaches.currentProgram->texCoords >= 0) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1853 | mCaches.bindTexCoordsVertexPointer(force, texCoords); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | mCaches.unbindIndicesBuffer(); |
| 1857 | } |
| 1858 | |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 1859 | void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices, |
| 1860 | const GLvoid* texCoords, const GLvoid* colors) { |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 1861 | bool force = mCaches.unbindMeshBuffer(); |
| 1862 | GLsizei stride = sizeof(ColorTextureVertex); |
| 1863 | |
| 1864 | mCaches.bindPositionVertexPointer(force, vertices, stride); |
| 1865 | if (mCaches.currentProgram->texCoords >= 0) { |
| 1866 | mCaches.bindTexCoordsVertexPointer(force, texCoords, stride); |
| 1867 | } |
| 1868 | int slot = mCaches.currentProgram->getAttrib("colors"); |
| 1869 | if (slot >= 0) { |
| 1870 | glEnableVertexAttribArray(slot); |
| 1871 | glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors); |
| 1872 | } |
| 1873 | |
| 1874 | mCaches.unbindIndicesBuffer(); |
| 1875 | } |
| 1876 | |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 1877 | void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices, |
| 1878 | const GLvoid* texCoords, GLuint vbo) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1879 | bool force = false; |
| 1880 | // If vbo is != 0 we want to treat the vertices parameter as an offset inside |
| 1881 | // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to |
| 1882 | // use the default VBO found in Caches |
| 1883 | if (!vertices || vbo) { |
| 1884 | force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
| 1885 | } else { |
| 1886 | force = mCaches.unbindMeshBuffer(); |
| 1887 | } |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 1888 | mCaches.bindQuadIndicesBuffer(); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1889 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1890 | mCaches.bindPositionVertexPointer(force, vertices); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1891 | if (mCaches.currentProgram->texCoords >= 0) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1892 | mCaches.bindTexCoordsVertexPointer(force, texCoords); |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1893 | } |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1894 | } |
| 1895 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 1896 | void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1897 | bool force = mCaches.unbindMeshBuffer(); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 1898 | mCaches.bindQuadIndicesBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1899 | mCaches.bindPositionVertexPointer(force, vertices, gVertexStride); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1902 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1903 | // Drawing |
| 1904 | /////////////////////////////////////////////////////////////////////////////// |
| 1905 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1906 | void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) { |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 1907 | // All the usual checks and setup operations (quickReject, setupDraw, etc.) |
| 1908 | // will be performed by the display list itself |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 1909 | if (renderNode && renderNode->isRenderable()) { |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 1910 | // compute 3d ordering |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 1911 | renderNode->computeOrdering(); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 1912 | if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1913 | startFrame(); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1914 | ReplayStateStruct replayStruct(*this, dirty, replayFlags); |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 1915 | renderNode->replay(replayStruct, 0); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1916 | return; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1917 | } |
| 1918 | |
Chris Craik | ef8d6f2 | 2014-12-17 11:10:28 -0800 | [diff] [blame] | 1919 | // Don't avoid overdraw when visualizing, since that makes it harder to |
| 1920 | // debug where it's coming from, and when the problem occurs. |
| 1921 | bool avoidOverdraw = !mCaches.debugOverdraw; |
Chris Craik | a5f0918 | 2014-12-17 15:11:30 -0800 | [diff] [blame] | 1922 | DeferredDisplayList deferredList(*mState.currentClipRect(), avoidOverdraw); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1923 | DeferStateStruct deferStruct(deferredList, *this, replayFlags); |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 1924 | renderNode->defer(deferStruct, 0); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 1925 | |
| 1926 | flushLayers(); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1927 | startFrame(); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 1928 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1929 | deferredList.flush(*this, dirty); |
| 1930 | } else { |
| 1931 | // Even if there is no drawing command(Ex: invisible), |
| 1932 | // it still needs startFrame to clear buffer and start tiling. |
| 1933 | startFrame(); |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1937 | void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, |
| 1938 | const SkPaint* paint) { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1939 | float x = left; |
| 1940 | float y = top; |
| 1941 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 1942 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 1943 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1944 | bool ignoreTransform = false; |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 1945 | if (currentTransform()->isPureTranslate()) { |
| 1946 | x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f); |
| 1947 | y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1948 | ignoreTransform = true; |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 1949 | |
| 1950 | texture->setFilter(GL_NEAREST, true); |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1951 | } else { |
Chris Craik | 67862524 | 2014-02-28 12:26:34 -0800 | [diff] [blame] | 1952 | texture->setFilter(getFilter(paint), true); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1955 | // No need to check for a UV mapper on the texture object, only ARGB_8888 |
| 1956 | // bitmaps get packed in the atlas |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 1957 | drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id, |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 1958 | paint, (GLvoid*) nullptr, (GLvoid*) gMeshTextureOffset, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1959 | GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 1962 | /** |
| 1963 | * Important note: this method is intended to draw batches of bitmaps and |
| 1964 | * will not set the scissor enable or dirty the current layer, if any. |
| 1965 | * The caller is responsible for properly dirtying the current layer. |
| 1966 | */ |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1967 | void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 1968 | int bitmapCount, TextureVertex* vertices, bool pureTranslate, |
| 1969 | const Rect& bounds, const SkPaint* paint) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1970 | mCaches.activeTexture(0); |
Romain Guy | 55b6f95 | 2013-06-27 15:27:09 -0700 | [diff] [blame] | 1971 | Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1972 | if (!texture) return; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1973 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1974 | const AutoTexture autoCleanup(texture); |
| 1975 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1976 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
Chris Craik | 67862524 | 2014-02-28 12:26:34 -0800 | [diff] [blame] | 1977 | texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1978 | |
| 1979 | const float x = (int) floorf(bounds.left + 0.5f); |
| 1980 | const float y = (int) floorf(bounds.top + 0.5f); |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 1981 | if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1982 | drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(), |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1983 | texture->id, paint, &vertices[0].x, &vertices[0].u, |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 1984 | GL_TRIANGLES, bitmapCount * 6, true, |
| 1985 | kModelViewMode_Translate, false); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1986 | } else { |
| 1987 | drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(), |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 1988 | texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u, |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 1989 | GL_TRIANGLES, bitmapCount * 6, false, true, 0, |
| 1990 | kModelViewMode_Translate, false); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1991 | } |
| 1992 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1993 | mDirty = true; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1996 | void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) { |
Chris Craik | 7964750 | 2014-08-06 13:42:24 -0700 | [diff] [blame] | 1997 | if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 1998 | return; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2001 | mCaches.activeTexture(0); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2002 | Texture* texture = getTexture(bitmap); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2003 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 2004 | const AutoTexture autoCleanup(texture); |
| 2005 | |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 2006 | if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) { |
Chris Craik | 7964750 | 2014-08-06 13:42:24 -0700 | [diff] [blame] | 2007 | drawAlphaBitmap(texture, 0, 0, paint); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 2008 | } else { |
Chris Craik | 7964750 | 2014-08-06 13:42:24 -0700 | [diff] [blame] | 2009 | drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 2010 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2011 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2012 | mDirty = true; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2015 | void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) { |
Chris Craik | 7964750 | 2014-08-06 13:42:24 -0700 | [diff] [blame] | 2016 | if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2017 | return; |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | mCaches.activeTexture(0); |
| 2021 | Texture* texture = mCaches.textureCache.getTransient(bitmap); |
| 2022 | const AutoTexture autoCleanup(texture); |
| 2023 | |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 2024 | if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) { |
Chris Craik | 7964750 | 2014-08-06 13:42:24 -0700 | [diff] [blame] | 2025 | drawAlphaBitmap(texture, 0, 0, paint); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2026 | } else { |
Chris Craik | 7964750 | 2014-08-06 13:42:24 -0700 | [diff] [blame] | 2027 | drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2028 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2029 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2030 | mDirty = true; |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2033 | void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2034 | const float* vertices, const int* colors, const SkPaint* paint) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2035 | if (!vertices || mState.currentlyIgnored()) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2036 | return; |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2037 | } |
| 2038 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 2039 | // TODO: use quickReject on bounds from vertices |
| 2040 | mCaches.enableScissor(); |
| 2041 | |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2042 | float left = FLT_MAX; |
| 2043 | float top = FLT_MAX; |
| 2044 | float right = FLT_MIN; |
| 2045 | float bottom = FLT_MIN; |
| 2046 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2047 | const uint32_t count = meshWidth * meshHeight * 6; |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2048 | |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 2049 | Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr |
| 2050 | mesh.setCapacity(count); |
| 2051 | ColorTextureVertex* vertex = mesh.editArray(); |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2052 | |
| 2053 | bool cleanupColors = false; |
| 2054 | if (!colors) { |
| 2055 | uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2056 | int* newColors = new int[colorsCount]; |
| 2057 | memset(newColors, 0xff, colorsCount * sizeof(int)); |
| 2058 | colors = newColors; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2059 | cleanupColors = true; |
| 2060 | } |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2061 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2062 | mCaches.activeTexture(0); |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 2063 | Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2064 | const UvMapper& mapper(getMapper(texture)); |
| 2065 | |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2066 | for (int32_t y = 0; y < meshHeight; y++) { |
| 2067 | for (int32_t x = 0; x < meshWidth; x++) { |
| 2068 | uint32_t i = (y * (meshWidth + 1) + x) * 2; |
| 2069 | |
| 2070 | float u1 = float(x) / meshWidth; |
| 2071 | float u2 = float(x + 1) / meshWidth; |
| 2072 | float v1 = float(y) / meshHeight; |
| 2073 | float v2 = float(y + 1) / meshHeight; |
| 2074 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2075 | mapper.map(u1, v1, u2, v2); |
| 2076 | |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2077 | int ax = i + (meshWidth + 1) * 2; |
| 2078 | int ay = ax + 1; |
| 2079 | int bx = i; |
| 2080 | int by = bx + 1; |
| 2081 | int cx = i + 2; |
| 2082 | int cy = cx + 1; |
| 2083 | int dx = i + (meshWidth + 1) * 2 + 2; |
| 2084 | int dy = dx + 1; |
| 2085 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2086 | ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]); |
| 2087 | ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]); |
| 2088 | ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]); |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2089 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2090 | ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]); |
| 2091 | ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]); |
| 2092 | ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]); |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2093 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2094 | left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); |
| 2095 | top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); |
| 2096 | right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); |
| 2097 | bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2098 | } |
| 2099 | } |
| 2100 | |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 2101 | if (quickRejectSetupScissor(left, top, right, bottom)) { |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2102 | if (cleanupColors) delete[] colors; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2103 | return; |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2106 | if (!texture) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2107 | texture = mCaches.textureCache.get(bitmap); |
| 2108 | if (!texture) { |
| 2109 | if (cleanupColors) delete[] colors; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2110 | return; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2111 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2112 | } |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2113 | const AutoTexture autoCleanup(texture); |
| 2114 | |
| 2115 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
Chris Craik | 67862524 | 2014-02-28 12:26:34 -0800 | [diff] [blame] | 2116 | texture->setFilter(getFilter(paint), true); |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2117 | |
| 2118 | int alpha; |
| 2119 | SkXfermode::Mode mode; |
| 2120 | getAlphaAndMode(paint, &alpha, &mode); |
| 2121 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2122 | float a = alpha / 255.0f; |
| 2123 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2124 | if (hasLayer()) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2125 | dirtyLayer(left, top, right, bottom, *currentTransform()); |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2126 | } |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2127 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2128 | setupDraw(); |
| 2129 | setupDrawWithTextureAndColor(); |
| 2130 | setupDrawColor(a, a, a, a); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2131 | setupDrawColorFilter(getColorFilter(paint)); |
| 2132 | setupDrawBlending(paint, true); |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2133 | setupDrawProgram(); |
| 2134 | setupDrawDirtyRegionsDisabled(); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 2135 | setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f); |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2136 | setupDrawTexture(texture->id); |
| 2137 | setupDrawPureColorUniforms(); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2138 | setupDrawColorFilterUniforms(getColorFilter(paint)); |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 2139 | setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r); |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2140 | |
| 2141 | glDrawArrays(GL_TRIANGLES, 0, count); |
| 2142 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2143 | int slot = mCaches.currentProgram->getAttrib("colors"); |
| 2144 | if (slot >= 0) { |
| 2145 | glDisableVertexAttribArray(slot); |
| 2146 | } |
| 2147 | |
| 2148 | if (cleanupColors) delete[] colors; |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2149 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2150 | mDirty = true; |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2151 | } |
| 2152 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2153 | void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2154 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 2155 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2156 | const SkPaint* paint) { |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 2157 | if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2158 | return; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2161 | mCaches.activeTexture(0); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2162 | Texture* texture = getTexture(bitmap); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2163 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 2164 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2165 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2166 | const float width = texture->width; |
| 2167 | const float height = texture->height; |
| 2168 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2169 | float u1 = fmax(0.0f, srcLeft / width); |
| 2170 | float v1 = fmax(0.0f, srcTop / height); |
| 2171 | float u2 = fmin(1.0f, srcRight / width); |
| 2172 | float v2 = fmin(1.0f, srcBottom / height); |
| 2173 | |
| 2174 | getMapper(texture).map(u1, v1, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2175 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 2176 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2177 | resetDrawTextureTexCoords(u1, v1, u2, v2); |
| 2178 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 2179 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 2180 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2181 | float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft); |
| 2182 | float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2183 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2184 | bool scaled = scaleX != 1.0f || scaleY != 1.0f; |
| 2185 | // Apply a scale transform on the canvas only when a shader is in use |
| 2186 | // Skia handles the ratio between the dst and src rects as a scale factor |
| 2187 | // when a shader is set |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 2188 | bool useScaleTransform = getShader(paint) && scaled; |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2189 | bool ignoreTransform = false; |
Romain Guy | b501498 | 2011-07-28 15:39:12 -0700 | [diff] [blame] | 2190 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2191 | if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) { |
| 2192 | float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f); |
| 2193 | float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2194 | |
| 2195 | dstRight = x + (dstRight - dstLeft); |
| 2196 | dstBottom = y + (dstBottom - dstTop); |
| 2197 | |
| 2198 | dstLeft = x; |
| 2199 | dstTop = y; |
| 2200 | |
Chris Craik | 67862524 | 2014-02-28 12:26:34 -0800 | [diff] [blame] | 2201 | texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2202 | ignoreTransform = true; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2203 | } else { |
Chris Craik | 67862524 | 2014-02-28 12:26:34 -0800 | [diff] [blame] | 2204 | texture->setFilter(getFilter(paint), true); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | if (CC_UNLIKELY(useScaleTransform)) { |
| 2208 | save(SkCanvas::kMatrix_SaveFlag); |
| 2209 | translate(dstLeft, dstTop); |
| 2210 | scale(scaleX, scaleY); |
| 2211 | |
| 2212 | dstLeft = 0.0f; |
| 2213 | dstTop = 0.0f; |
| 2214 | |
| 2215 | dstRight = srcRight - srcLeft; |
| 2216 | dstBottom = srcBottom - srcTop; |
| 2217 | } |
| 2218 | |
Mike Reed | 1103b32 | 2014-07-08 12:36:44 -0400 | [diff] [blame] | 2219 | if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) { |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2220 | drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2221 | texture->id, paint, |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 2222 | &mMeshVertices[0].x, &mMeshVertices[0].u, |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2223 | GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform); |
| 2224 | } else { |
| 2225 | drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2226 | texture->id, paint, texture->blend, |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 2227 | &mMeshVertices[0].x, &mMeshVertices[0].u, |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2228 | GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform); |
| 2229 | } |
| 2230 | |
| 2231 | if (CC_UNLIKELY(useScaleTransform)) { |
| 2232 | restore(); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2233 | } |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2234 | |
| 2235 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2236 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2237 | mDirty = true; |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2240 | void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2241 | float left, float top, float right, float bottom, const SkPaint* paint) { |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 2242 | if (quickRejectSetupScissor(left, top, right, bottom)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2243 | return; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2244 | } |
| 2245 | |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 2246 | AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2247 | const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(), |
| 2248 | right - left, bottom - top, patch); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 2249 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2250 | drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint); |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 2251 | } |
| 2252 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2253 | void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2254 | AssetAtlas::Entry* entry, float left, float top, float right, float bottom, |
| 2255 | const SkPaint* paint) { |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 2256 | if (quickRejectSetupScissor(left, top, right, bottom)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2257 | return; |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2260 | if (CC_LIKELY(mesh && mesh->verticesCount > 0)) { |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 2261 | mCaches.activeTexture(0); |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 2262 | Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2263 | if (!texture) return; |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 2264 | const AutoTexture autoCleanup(texture); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2265 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 2266 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 2267 | texture->setFilter(GL_LINEAR, true); |
| 2268 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2269 | const bool pureTranslate = currentTransform()->isPureTranslate(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2270 | // 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] | 2271 | if (hasLayer() && mesh->hasEmptyQuads) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2272 | const float offsetX = left + currentTransform()->getTranslateX(); |
| 2273 | const float offsetY = top + currentTransform()->getTranslateY(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2274 | const size_t count = mesh->quads.size(); |
| 2275 | for (size_t i = 0; i < count; i++) { |
Romain Guy | 8ab4079 | 2010-12-07 13:30:10 -0800 | [diff] [blame] | 2276 | const Rect& bounds = mesh->quads.itemAt(i); |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2277 | if (CC_LIKELY(pureTranslate)) { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 2278 | const float x = (int) floorf(bounds.left + offsetX + 0.5f); |
| 2279 | const float y = (int) floorf(bounds.top + offsetY + 0.5f); |
| 2280 | dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2281 | } else { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 2282 | dirtyLayer(left + bounds.left, top + bounds.top, |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2283 | left + bounds.right, top + bounds.bottom, *currentTransform()); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2284 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 2288 | bool ignoreTransform = false; |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2289 | if (CC_LIKELY(pureTranslate)) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2290 | const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f); |
| 2291 | const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2292 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2293 | right = x + right - left; |
| 2294 | bottom = y + bottom - top; |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 2295 | left = x; |
| 2296 | top = y; |
| 2297 | ignoreTransform = true; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2298 | } |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2299 | drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint, |
| 2300 | texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset, |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 2301 | GL_TRIANGLES, mesh->indexCount, false, ignoreTransform, |
| 2302 | mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads); |
Romain Guy | 054dc18 | 2010-10-15 17:55:25 -0700 | [diff] [blame] | 2303 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2304 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2305 | mDirty = true; |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2308 | /** |
| 2309 | * Important note: this method is intended to draw batches of 9-patch objects and |
| 2310 | * will not set the scissor enable or dirty the current layer, if any. |
| 2311 | * The caller is responsible for properly dirtying the current layer. |
| 2312 | */ |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2313 | void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2314 | TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) { |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2315 | mCaches.activeTexture(0); |
| 2316 | Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2317 | if (!texture) return; |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2318 | const AutoTexture autoCleanup(texture); |
| 2319 | |
| 2320 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 2321 | texture->setFilter(GL_LINEAR, true); |
| 2322 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2323 | drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint, |
| 2324 | texture->blend, &vertices[0].x, &vertices[0].u, |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 2325 | GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false); |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2326 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2327 | mDirty = true; |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2328 | } |
| 2329 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2330 | void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY, |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 2331 | const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) { |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 2332 | // not missing call to quickReject/dirtyLayer, always done at a higher level |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2333 | if (!vertexBuffer.getVertexCount()) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2334 | // no vertices to draw |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2335 | return; |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2336 | } |
| 2337 | |
Chris Craik | 0d5ac95 | 2014-07-15 13:01:02 -0700 | [diff] [blame] | 2338 | Rect bounds(vertexBuffer.getBounds()); |
| 2339 | bounds.translate(translateX, translateY); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 2340 | dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform()); |
| 2341 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2342 | int color = paint->getColor(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2343 | bool isAA = paint->isAntiAlias(); |
| 2344 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2345 | setupDraw(); |
| 2346 | setupDrawNoTexture(); |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 2347 | if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp)); |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2348 | setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2349 | setupDrawColorFilter(getColorFilter(paint)); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 2350 | setupDrawShader(getShader(paint)); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2351 | setupDrawBlending(paint, isAA); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2352 | setupDrawProgram(); |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 2353 | setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset), |
| 2354 | translateX, translateY, 0, 0); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 2355 | setupDrawColorUniforms(getShader(paint)); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2356 | setupDrawColorFilterUniforms(getColorFilter(paint)); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 2357 | setupDrawShaderUniforms(getShader(paint)); |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2358 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 2359 | const void* vertices = vertexBuffer.getBuffer(); |
Andreas Gampe | edaecc1 | 2014-11-10 20:54:07 -0800 | [diff] [blame] | 2360 | mCaches.unbindMeshBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2361 | mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2362 | mCaches.resetTexCoordsVertexPointer(); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 2363 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2364 | int alphaSlot = -1; |
| 2365 | if (isAA) { |
| 2366 | void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset; |
| 2367 | alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha"); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2368 | // 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] | 2369 | glEnableVertexAttribArray(alphaSlot); |
| 2370 | glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2371 | } |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 2372 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 2373 | const VertexBuffer::Mode mode = vertexBuffer.getMode(); |
| 2374 | if (mode == VertexBuffer::kStandard) { |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 2375 | mCaches.unbindIndicesBuffer(); |
| 2376 | glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount()); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 2377 | } else if (mode == VertexBuffer::kOnePolyRingShadow) { |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 2378 | mCaches.bindShadowIndicesBuffer(); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2379 | glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, |
| 2380 | GL_UNSIGNED_SHORT, nullptr); |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 2381 | } else if (mode == VertexBuffer::kTwoPolyRingShadow) { |
ztenghui | 50ecf84 | 2014-03-11 16:52:30 -0700 | [diff] [blame] | 2382 | mCaches.bindShadowIndicesBuffer(); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2383 | glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, |
| 2384 | GL_UNSIGNED_SHORT, nullptr); |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 2385 | } else if (mode == VertexBuffer::kIndices) { |
| 2386 | mCaches.unbindIndicesBuffer(); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2387 | glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(), |
| 2388 | GL_UNSIGNED_SHORT, vertexBuffer.getIndices()); |
ztenghui | 63d41ab | 2014-02-14 13:13:41 -0800 | [diff] [blame] | 2389 | } |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 2390 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2391 | if (isAA) { |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 2392 | glDisableVertexAttribArray(alphaSlot); |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 2393 | } |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2394 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2395 | mDirty = true; |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | /** |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2399 | * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to |
| 2400 | * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in |
| 2401 | * screen space in all directions. However, instead of using a fragment shader to compute the |
| 2402 | * translucency of the color from its position, we simply use a varying parameter to define how far |
| 2403 | * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used. |
| 2404 | * |
| 2405 | * Doesn't yet support joins, caps, or path effects. |
| 2406 | */ |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2407 | void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2408 | VertexBuffer vertexBuffer; |
| 2409 | // TODO: try clipping large paths to viewport |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2410 | PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2411 | drawVertexBuffer(vertexBuffer, paint); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | /** |
| 2415 | * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha |
| 2416 | * and additional geometry for defining an alpha slope perimeter. |
| 2417 | * |
| 2418 | * Using GL_LINES can be difficult because the rasterization rules for those lines produces some |
| 2419 | * unexpected results, and may vary between hardware devices. Previously we used a varying-base |
| 2420 | * in-shader alpha region, but found it to be taxing on some GPUs. |
| 2421 | * |
| 2422 | * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce |
| 2423 | * memory transfer by removing need for degenerate vertices. |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2424 | */ |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2425 | void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2426 | if (mState.currentlyIgnored() || count < 4) return; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2427 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2428 | count &= ~0x3; // round down to nearest four |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2429 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2430 | VertexBuffer buffer; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 2431 | PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer); |
| 2432 | const Rect& bounds = buffer.getBounds(); |
Romain Guy | d71ff91d | 2013-02-08 13:46:40 -0800 | [diff] [blame] | 2433 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 2434 | if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2435 | return; |
Romain Guy | d71ff91d | 2013-02-08 13:46:40 -0800 | [diff] [blame] | 2436 | } |
| 2437 | |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 2438 | int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2439 | drawVertexBuffer(buffer, paint, displayFlags); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2442 | void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2443 | if (mState.currentlyIgnored() || count < 2) return; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2444 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2445 | count &= ~0x1; // round down to nearest two |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2446 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2447 | VertexBuffer buffer; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 2448 | PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer); |
Romain Guy | d71ff91d | 2013-02-08 13:46:40 -0800 | [diff] [blame] | 2449 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 2450 | const Rect& bounds = buffer.getBounds(); |
| 2451 | if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2452 | return; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 2455 | int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset; |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2456 | drawVertexBuffer(buffer, paint, displayFlags); |
| 2457 | |
| 2458 | mDirty = true; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2459 | } |
| 2460 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2461 | void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 2462 | // No need to check against the clip, we fill the clip region |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2463 | if (mState.currentlyIgnored()) return; |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 2464 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2465 | Rect clip(*mState.currentClipRect()); |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 2466 | clip.snapToPixelBoundaries(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2467 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2468 | SkPaint paint; |
| 2469 | paint.setColor(color); |
| 2470 | paint.setXfermodeMode(mode); |
| 2471 | |
| 2472 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2473 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2474 | mDirty = true; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 2475 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 2476 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2477 | void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2478 | const SkPaint* paint) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2479 | if (!texture) return; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2480 | const AutoTexture autoCleanup(texture); |
| 2481 | |
| 2482 | const float x = left + texture->left - texture->offset; |
| 2483 | const float y = top + texture->top - texture->offset; |
| 2484 | |
| 2485 | drawPathTexture(texture, x, y, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2486 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2487 | mDirty = true; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2488 | } |
| 2489 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2490 | void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2491 | float rx, float ry, const SkPaint* p) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2492 | if (mState.currentlyIgnored() |
Chris Craik | 947eabf | 2014-08-19 10:21:12 -0700 | [diff] [blame] | 2493 | || quickRejectSetupScissor(left, top, right, bottom, p) |
| 2494 | || paintWillNotDraw(*p)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2495 | return; |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2496 | } |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2497 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2498 | if (p->getPathEffect() != nullptr) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2499 | mCaches.activeTexture(0); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2500 | const PathTexture* texture = mCaches.pathCache.getRoundRect( |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2501 | right - left, bottom - top, rx, ry, p); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2502 | drawShape(left, top, texture, p); |
| 2503 | } else { |
| 2504 | const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect( |
| 2505 | *currentTransform(), *p, right - left, bottom - top, rx, ry); |
| 2506 | drawVertexBuffer(left, top, *vertexBuffer, p); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2507 | } |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2508 | } |
| 2509 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2510 | void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2511 | if (mState.currentlyIgnored() |
Chris Craik | 947eabf | 2014-08-19 10:21:12 -0700 | [diff] [blame] | 2512 | || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p) |
| 2513 | || paintWillNotDraw(*p)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2514 | return; |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2515 | } |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2516 | if (p->getPathEffect() != nullptr) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2517 | mCaches.activeTexture(0); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2518 | const PathTexture* texture = mCaches.pathCache.getCircle(radius, p); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2519 | drawShape(x - radius, y - radius, texture, p); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2520 | } else { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2521 | SkPath path; |
| 2522 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2523 | path.addCircle(x, y, radius + p->getStrokeWidth() / 2); |
| 2524 | } else { |
| 2525 | path.addCircle(x, y, radius); |
| 2526 | } |
| 2527 | drawConvexPath(path, p); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2528 | } |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2529 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2530 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2531 | void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2532 | const SkPaint* p) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2533 | if (mState.currentlyIgnored() |
Chris Craik | 947eabf | 2014-08-19 10:21:12 -0700 | [diff] [blame] | 2534 | || quickRejectSetupScissor(left, top, right, bottom, p) |
| 2535 | || paintWillNotDraw(*p)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2536 | return; |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2537 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2538 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2539 | if (p->getPathEffect() != nullptr) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2540 | mCaches.activeTexture(0); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2541 | const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2542 | drawShape(left, top, texture, p); |
| 2543 | } else { |
| 2544 | SkPath path; |
| 2545 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
| 2546 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2547 | rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); |
| 2548 | } |
| 2549 | path.addOval(rect); |
| 2550 | drawConvexPath(path, p); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2551 | } |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2552 | } |
| 2553 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2554 | void OpenGLRenderer::drawArc(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2555 | float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2556 | if (mState.currentlyIgnored() |
Chris Craik | 947eabf | 2014-08-19 10:21:12 -0700 | [diff] [blame] | 2557 | || quickRejectSetupScissor(left, top, right, bottom, p) |
| 2558 | || paintWillNotDraw(*p)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2559 | return; |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 2560 | } |
| 2561 | |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2562 | // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180) |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2563 | if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) { |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2564 | mCaches.activeTexture(0); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2565 | const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top, |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2566 | startAngle, sweepAngle, useCenter, p); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2567 | drawShape(left, top, texture, p); |
| 2568 | return; |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2569 | } |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2570 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
| 2571 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2572 | rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); |
| 2573 | } |
| 2574 | |
| 2575 | SkPath path; |
| 2576 | if (useCenter) { |
| 2577 | path.moveTo(rect.centerX(), rect.centerY()); |
| 2578 | } |
| 2579 | path.arcTo(rect, startAngle, sweepAngle, !useCenter); |
| 2580 | if (useCenter) { |
| 2581 | path.close(); |
| 2582 | } |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2583 | drawConvexPath(path, p); |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 2584 | } |
| 2585 | |
Romain Guy | cf8675e | 2012-10-02 12:32:25 -0700 | [diff] [blame] | 2586 | // See SkPaintDefaults.h |
| 2587 | #define SkPaintDefaults_MiterLimit SkIntToScalar(4) |
| 2588 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2589 | void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2590 | const SkPaint* p) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2591 | if (mState.currentlyIgnored() |
Chris Craik | 947eabf | 2014-08-19 10:21:12 -0700 | [diff] [blame] | 2592 | || quickRejectSetupScissor(left, top, right, bottom, p) |
| 2593 | || paintWillNotDraw(*p)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2594 | return; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2595 | } |
| 2596 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2597 | if (p->getStyle() != SkPaint::kFill_Style) { |
Romain Guy | cf8675e | 2012-10-02 12:32:25 -0700 | [diff] [blame] | 2598 | // only fill style is supported by drawConvexPath, since others have to handle joins |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2599 | if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join || |
Romain Guy | cf8675e | 2012-10-02 12:32:25 -0700 | [diff] [blame] | 2600 | p->getStrokeMiter() != SkPaintDefaults_MiterLimit) { |
| 2601 | mCaches.activeTexture(0); |
| 2602 | const PathTexture* texture = |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2603 | mCaches.pathCache.getRect(right - left, bottom - top, p); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2604 | drawShape(left, top, texture, p); |
| 2605 | } else { |
| 2606 | SkPath path; |
| 2607 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
| 2608 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2609 | rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); |
| 2610 | } |
| 2611 | path.addRect(rect); |
| 2612 | drawConvexPath(path, p); |
Romain Guy | cf8675e | 2012-10-02 12:32:25 -0700 | [diff] [blame] | 2613 | } |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2614 | } else { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2615 | if (p->isAntiAlias() && !currentTransform()->isSimple()) { |
| 2616 | SkPath path; |
| 2617 | path.addRect(left, top, right, bottom); |
| 2618 | drawConvexPath(path, p); |
| 2619 | } else { |
| 2620 | drawColorRect(left, top, right, bottom, p); |
| 2621 | |
| 2622 | mDirty = true; |
| 2623 | } |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2624 | } |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 2625 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 2626 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2627 | void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text, |
| 2628 | int bytesCount, int count, const float* positions, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2629 | FontRenderer& fontRenderer, int alpha, float x, float y) { |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2630 | mCaches.activeTexture(0); |
| 2631 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 2632 | TextShadow textShadow; |
| 2633 | if (!getTextShadow(paint, &textShadow)) { |
| 2634 | LOG_ALWAYS_FATAL("failed to query shadow attributes"); |
| 2635 | } |
| 2636 | |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2637 | // NOTE: The drop shadow will not perform gamma correction |
| 2638 | // if shader-based correction is enabled |
| 2639 | mCaches.dropShadowCache.setFontRenderer(fontRenderer); |
| 2640 | const ShadowTexture* shadow = mCaches.dropShadowCache.get( |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 2641 | paint, text, bytesCount, count, textShadow.radius, positions); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 2642 | // If the drop shadow exceeds the max texture size or couldn't be |
| 2643 | // allocated, skip drawing |
| 2644 | if (!shadow) return; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2645 | const AutoTexture autoCleanup(shadow); |
| 2646 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 2647 | const float sx = x - shadow->left + textShadow.dx; |
| 2648 | const float sy = y - shadow->top + textShadow.dy; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2649 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2650 | const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha; |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 2651 | if (getShader(paint)) { |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 2652 | textShadow.color = SK_ColorWHITE; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | setupDraw(); |
| 2656 | setupDrawWithTexture(true); |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 2657 | setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2658 | setupDrawColorFilter(getColorFilter(paint)); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 2659 | setupDrawShader(getShader(paint)); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2660 | setupDrawBlending(paint, true); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2661 | setupDrawProgram(); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 2662 | setupDrawModelView(kModelViewMode_TranslateAndScale, false, |
| 2663 | sx, sy, sx + shadow->width, sy + shadow->height); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2664 | setupDrawTexture(shadow->id); |
| 2665 | setupDrawPureColorUniforms(); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2666 | setupDrawColorFilterUniforms(getColorFilter(paint)); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 2667 | setupDrawShaderUniforms(getShader(paint)); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2668 | setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2669 | |
| 2670 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 2671 | } |
| 2672 | |
Romain Guy | 768bffc | 2013-02-27 13:50:45 -0800 | [diff] [blame] | 2673 | bool OpenGLRenderer::canSkipText(const SkPaint* paint) const { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2674 | float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha; |
Romain Guy | 768bffc | 2013-02-27 13:50:45 -0800 | [diff] [blame] | 2675 | return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode; |
| 2676 | } |
| 2677 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2678 | void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2679 | const float* positions, const SkPaint* paint) { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2680 | if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2681 | return; |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2682 | } |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2683 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2684 | // NOTE: Skia does not support perspective transform on drawPosText yet |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2685 | if (!currentTransform()->isSimple()) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2686 | return; |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2687 | } |
| 2688 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 2689 | mCaches.enableScissor(); |
| 2690 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2691 | float x = 0.0f; |
| 2692 | float y = 0.0f; |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2693 | const bool pureTranslate = currentTransform()->isPureTranslate(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2694 | if (pureTranslate) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2695 | x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f); |
| 2696 | y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2697 | } |
| 2698 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 2699 | FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 2700 | fontRenderer.setFont(paint, SkMatrix::I()); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2701 | |
| 2702 | int alpha; |
| 2703 | SkXfermode::Mode mode; |
| 2704 | getAlphaAndMode(paint, &alpha, &mode); |
| 2705 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 2706 | if (CC_UNLIKELY(hasTextShadow(paint))) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 2707 | drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2708 | alpha, 0.0f, 0.0f); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2709 | } |
| 2710 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2711 | // Pick the appropriate texture filtering |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2712 | bool linearFilter = currentTransform()->changesBounds(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2713 | if (pureTranslate && !linearFilter) { |
| 2714 | linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; |
| 2715 | } |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2716 | fontRenderer.setTextureFiltering(linearFilter); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2717 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2718 | const Rect* clip = pureTranslate ? writableSnapshot()->clipRect : &writableSnapshot()->getLocalClip(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2719 | Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
| 2720 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2721 | const bool hasActiveLayer = hasLayer(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2722 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 2723 | TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2724 | if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2725 | positions, hasActiveLayer ? &bounds : nullptr, &functor)) { |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2726 | if (hasActiveLayer) { |
| 2727 | if (!pureTranslate) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2728 | currentTransform()->mapRect(bounds); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2729 | } |
| 2730 | dirtyLayerUnchecked(bounds, getRegion()); |
| 2731 | } |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2732 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2733 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2734 | mDirty = true; |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2735 | } |
| 2736 | |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 2737 | bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const { |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2738 | if (CC_LIKELY(transform.isPureTranslate())) { |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 2739 | outMatrix->setIdentity(); |
| 2740 | return false; |
| 2741 | } else if (CC_UNLIKELY(transform.isPerspective())) { |
| 2742 | outMatrix->setIdentity(); |
| 2743 | return true; |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2744 | } |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 2745 | |
| 2746 | /** |
Chris Craik | a736cd9 | 2014-08-04 13:18:38 -0700 | [diff] [blame] | 2747 | * Input is a non-perspective, scaling transform. Generate a scale-only transform, |
| 2748 | * with values rounded to the nearest int. |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 2749 | */ |
| 2750 | float sx, sy; |
| 2751 | transform.decomposeScale(sx, sy); |
Chris Craik | a736cd9 | 2014-08-04 13:18:38 -0700 | [diff] [blame] | 2752 | outMatrix->setScale( |
| 2753 | roundf(fmaxf(1.0f, sx)), |
| 2754 | roundf(fmaxf(1.0f, sy))); |
| 2755 | return true; |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2756 | } |
| 2757 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2758 | int OpenGLRenderer::getSaveCount() const { |
| 2759 | return mState.getSaveCount(); |
| 2760 | } |
| 2761 | |
| 2762 | int OpenGLRenderer::save(int flags) { |
| 2763 | return mState.save(flags); |
| 2764 | } |
| 2765 | |
| 2766 | void OpenGLRenderer::restore() { |
| 2767 | return mState.restore(); |
| 2768 | } |
| 2769 | |
| 2770 | void OpenGLRenderer::restoreToCount(int saveCount) { |
| 2771 | return mState.restoreToCount(saveCount); |
| 2772 | } |
| 2773 | |
| 2774 | void OpenGLRenderer::translate(float dx, float dy, float dz) { |
| 2775 | return mState.translate(dx, dy, dz); |
| 2776 | } |
| 2777 | |
| 2778 | void OpenGLRenderer::rotate(float degrees) { |
| 2779 | return mState.rotate(degrees); |
| 2780 | } |
| 2781 | |
| 2782 | void OpenGLRenderer::scale(float sx, float sy) { |
| 2783 | return mState.scale(sx, sy); |
| 2784 | } |
| 2785 | |
| 2786 | void OpenGLRenderer::skew(float sx, float sy) { |
| 2787 | return mState.skew(sx, sy); |
| 2788 | } |
| 2789 | |
| 2790 | void OpenGLRenderer::setMatrix(const Matrix4& matrix) { |
| 2791 | mState.setMatrix(matrix); |
| 2792 | } |
| 2793 | |
| 2794 | void OpenGLRenderer::concatMatrix(const Matrix4& matrix) { |
| 2795 | mState.concatMatrix(matrix); |
| 2796 | } |
| 2797 | |
| 2798 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 2799 | return mState.clipRect(left, top, right, bottom, op); |
| 2800 | } |
| 2801 | |
| 2802 | bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) { |
| 2803 | return mState.clipPath(path, op); |
| 2804 | } |
| 2805 | |
| 2806 | bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) { |
| 2807 | return mState.clipRegion(region, op); |
| 2808 | } |
| 2809 | |
| 2810 | void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) { |
| 2811 | mState.setClippingOutline(allocator, outline); |
| 2812 | } |
| 2813 | |
| 2814 | void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator, |
| 2815 | const Rect& rect, float radius, bool highPriority) { |
| 2816 | mState.setClippingRoundRect(allocator, rect, radius, highPriority); |
| 2817 | } |
| 2818 | |
| 2819 | |
| 2820 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2821 | void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2822 | const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds, |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2823 | DrawOpMode drawOpMode) { |
| 2824 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2825 | if (drawOpMode == kDrawOpMode_Immediate) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 2826 | // The checks for corner-case ignorable text and quick rejection is only done for immediate |
| 2827 | // drawing as ops from DeferredDisplayList are already filtered for these |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2828 | if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) || |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 2829 | quickRejectSetupScissor(bounds)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2830 | return; |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 2831 | } |
Romain Guy | cac5fd3 | 2011-12-01 20:08:50 -0800 | [diff] [blame] | 2832 | } |
| 2833 | |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 2834 | const float oldX = x; |
| 2835 | const float oldY = y; |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2836 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2837 | const mat4& transform = *currentTransform(); |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2838 | const bool pureTranslate = transform.isPureTranslate(); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 2839 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2840 | if (CC_LIKELY(pureTranslate)) { |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2841 | x = (int) floorf(x + transform.getTranslateX() + 0.5f); |
| 2842 | y = (int) floorf(y + transform.getTranslateY() + 0.5f); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2843 | } |
| 2844 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2845 | int alpha; |
| 2846 | SkXfermode::Mode mode; |
| 2847 | getAlphaAndMode(paint, &alpha, &mode); |
Romain Guy | 9d13fe25 | 2010-10-15 16:06:03 -0700 | [diff] [blame] | 2848 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 2849 | FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); |
| 2850 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 2851 | if (CC_UNLIKELY(hasTextShadow(paint))) { |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 2852 | fontRenderer.setFont(paint, SkMatrix::I()); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 2853 | drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2854 | alpha, oldX, oldY); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 2855 | } |
| 2856 | |
Romain Guy | 19d4dd8 | 2013-03-04 11:14:26 -0800 | [diff] [blame] | 2857 | const bool hasActiveLayer = hasLayer(); |
| 2858 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2859 | // We only pass a partial transform to the font renderer. That partial |
| 2860 | // matrix defines how glyphs are rasterized. Typically we want glyphs |
| 2861 | // to be rasterized at their final size on screen, which means the partial |
| 2862 | // matrix needs to take the scale factor into account. |
| 2863 | // When a partial matrix is used to transform glyphs during rasterization, |
| 2864 | // the mesh is generated with the inverse transform (in the case of scale, |
| 2865 | // the mesh is generated at 1.0 / scale for instance.) This allows us to |
| 2866 | // apply the full transform matrix at draw time in the vertex shader. |
| 2867 | // Applying the full matrix in the shader is the easiest way to handle |
| 2868 | // rotation and perspective and allows us to always generated quads in the |
| 2869 | // font renderer which greatly simplifies the code, clipping in particular. |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 2870 | SkMatrix fontTransform; |
| 2871 | bool linearFilter = findBestFontTransform(transform, &fontTransform) |
| 2872 | || fabs(y - (int) y) > 0.0f |
| 2873 | || fabs(x - (int) x) > 0.0f; |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2874 | fontRenderer.setFont(paint, fontTransform); |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2875 | fontRenderer.setTextureFiltering(linearFilter); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2876 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2877 | // TODO: Implement better clipping for scaled/rotated text |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2878 | const Rect* clip = !pureTranslate ? nullptr : mState.currentClipRect(); |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 2879 | Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2880 | |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2881 | bool status; |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 2882 | TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2883 | |
| 2884 | // don't call issuedrawcommand, do it at end of batch |
| 2885 | bool forceFinish = (drawOpMode != kDrawOpMode_Defer); |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 2886 | if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) { |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 2887 | SkPaint paintCopy(*paint); |
| 2888 | paintCopy.setTextAlign(SkPaint::kLeft_Align); |
| 2889 | status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y, |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2890 | positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish); |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2891 | } else { |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 2892 | status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2893 | positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish); |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2894 | } |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2895 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2896 | if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) { |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2897 | if (!pureTranslate) { |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 2898 | transform.mapRect(layerBounds); |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 2899 | } |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 2900 | dirtyLayerUnchecked(layerBounds, getRegion()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2901 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 2902 | |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 2903 | drawTextDecorations(totalAdvance, oldX, oldY, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2904 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2905 | mDirty = true; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 2906 | } |
| 2907 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2908 | void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 2909 | const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2910 | if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2911 | return; |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2912 | } |
| 2913 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 2914 | // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics |
| 2915 | mCaches.enableScissor(); |
| 2916 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 2917 | FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 2918 | fontRenderer.setFont(paint, SkMatrix::I()); |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2919 | fontRenderer.setTextureFiltering(true); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2920 | |
| 2921 | int alpha; |
| 2922 | SkXfermode::Mode mode; |
| 2923 | getAlphaAndMode(paint, &alpha, &mode); |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 2924 | TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 2925 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2926 | const Rect* clip = &writableSnapshot()->getLocalClip(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2927 | 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] | 2928 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2929 | const bool hasActiveLayer = hasLayer(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2930 | |
| 2931 | if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path, |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2932 | hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) { |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2933 | if (hasActiveLayer) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 2934 | currentTransform()->mapRect(bounds); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2935 | dirtyLayerUnchecked(bounds, getRegion()); |
| 2936 | } |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 2937 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2938 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2939 | mDirty = true; |
Romain Guy | 325740f | 2012-02-24 16:48:34 -0800 | [diff] [blame] | 2940 | } |
| 2941 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2942 | void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 2943 | if (mState.currentlyIgnored()) return; |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 2944 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2945 | mCaches.activeTexture(0); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2946 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2947 | const PathTexture* texture = mCaches.pathCache.get(path, paint); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2948 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 2949 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2950 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 2951 | const float x = texture->left - texture->offset; |
| 2952 | const float y = texture->top - texture->offset; |
| 2953 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2954 | drawPathTexture(texture, x, y, paint); |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2955 | mDirty = true; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2958 | void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) { |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 2959 | if (!layer) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2960 | return; |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 2961 | } |
| 2962 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2963 | mat4* transform = nullptr; |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 2964 | if (layer->isTextureLayer()) { |
| 2965 | transform = &layer->getTransform(); |
| 2966 | if (!transform->isIdentity()) { |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 2967 | save(SkCanvas::kMatrix_SaveFlag); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 2968 | concatMatrix(*transform); |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 2969 | } |
| 2970 | } |
| 2971 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 2972 | bool clipRequired = false; |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 2973 | const bool rejected = mState.calculateQuickRejectForScissor( |
| 2974 | x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), |
| 2975 | &clipRequired, nullptr, false); |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 2976 | |
| 2977 | if (rejected) { |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 2978 | if (transform && !transform->isIdentity()) { |
| 2979 | restore(); |
| 2980 | } |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 2981 | return; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2982 | } |
| 2983 | |
Chris Craik | 62d307c | 2014-07-29 10:35:13 -0700 | [diff] [blame] | 2984 | EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y, |
| 2985 | x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired); |
| 2986 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 2987 | updateLayer(layer, true); |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 2988 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 2989 | mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired); |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2990 | mCaches.activeTexture(0); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2991 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2992 | if (CC_LIKELY(!layer->region.isEmpty())) { |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2993 | if (layer->region.isRect()) { |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 2994 | DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, |
| 2995 | composeLayerRect(layer, layer->regionRect)); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2996 | } else if (layer->mesh) { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 2997 | |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 2998 | const float a = getLayerAlpha(layer); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2999 | setupDraw(); |
| 3000 | setupDrawWithTexture(); |
Romain Guy | 8168396 | 2011-01-24 20:40:18 -0800 | [diff] [blame] | 3001 | setupDrawColor(a, a, a, a); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3002 | setupDrawColorFilter(layer->getColorFilter()); |
| 3003 | setupDrawBlending(layer); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3004 | setupDrawProgram(); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3005 | setupDrawPureColorUniforms(); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3006 | setupDrawColorFilterUniforms(layer->getColorFilter()); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 3007 | setupDrawTexture(layer->getTexture()); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 3008 | if (CC_LIKELY(currentTransform()->isPureTranslate())) { |
| 3009 | int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f); |
| 3010 | int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 3011 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3012 | layer->setFilter(GL_NEAREST); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3013 | setupDrawModelView(kModelViewMode_Translate, false, tx, ty, |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 3014 | tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 3015 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3016 | layer->setFilter(GL_LINEAR); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3017 | setupDrawModelView(kModelViewMode_Translate, false, x, y, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 3018 | x + layer->layer.getWidth(), y + layer->layer.getHeight()); |
| 3019 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 3020 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 3021 | TextureVertex* mesh = &layer->mesh[0]; |
| 3022 | GLsizei elementsCount = layer->meshElementCount; |
| 3023 | |
| 3024 | while (elementsCount > 0) { |
| 3025 | GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6); |
| 3026 | |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 3027 | setupDrawMeshIndices(&mesh[0].x, &mesh[0].u); |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 3028 | DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 3029 | glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr)); |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 3030 | |
| 3031 | elementsCount -= drawCount; |
| 3032 | // Though there are 4 vertices in a quad, we use 6 indices per |
| 3033 | // quad to draw with GL_TRIANGLES |
| 3034 | mesh += (drawCount / 6) * 4; |
| 3035 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 3036 | |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 3037 | #if DEBUG_LAYERS_AS_REGIONS |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 3038 | drawRegionRectsDebug(layer->region); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 3039 | #endif |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3040 | } |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 3041 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 3042 | if (layer->debugDrawUpdate) { |
| 3043 | layer->debugDrawUpdate = false; |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3044 | |
| 3045 | SkPaint paint; |
| 3046 | paint.setColor(0x7f00ff00); |
| 3047 | drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 3048 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 3049 | } |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 3050 | layer->hasDrawnSinceUpdate = true; |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3051 | |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 3052 | if (transform && !transform->isIdentity()) { |
| 3053 | restore(); |
| 3054 | } |
| 3055 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3056 | mDirty = true; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 3057 | } |
| 3058 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3059 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 3060 | // Draw filters |
| 3061 | /////////////////////////////////////////////////////////////////////////////// |
Derek Sollenberger | 09c2d4f | 2014-10-15 09:21:10 -0400 | [diff] [blame] | 3062 | void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) { |
| 3063 | // We should never get here since we apply the draw filter when stashing |
| 3064 | // the paints in the DisplayList. |
| 3065 | LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters"); |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3069 | // Drawing implementation |
| 3070 | /////////////////////////////////////////////////////////////////////////////// |
| 3071 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 3072 | Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) { |
John Reck | ebd5261 | 2014-12-10 16:47:36 -0800 | [diff] [blame] | 3073 | Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3074 | if (!texture) { |
| 3075 | return mCaches.textureCache.get(bitmap); |
| 3076 | } |
| 3077 | return texture; |
| 3078 | } |
| 3079 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 3080 | void OpenGLRenderer::drawPathTexture(const PathTexture* texture, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 3081 | float x, float y, const SkPaint* paint) { |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 3082 | if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) { |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 3083 | return; |
| 3084 | } |
| 3085 | |
| 3086 | int alpha; |
| 3087 | SkXfermode::Mode mode; |
| 3088 | getAlphaAndMode(paint, &alpha, &mode); |
| 3089 | |
| 3090 | setupDraw(); |
| 3091 | setupDrawWithTexture(true); |
| 3092 | setupDrawAlpha8Color(paint->getColor(), alpha); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3093 | setupDrawColorFilter(getColorFilter(paint)); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3094 | setupDrawShader(getShader(paint)); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3095 | setupDrawBlending(paint, true); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 3096 | setupDrawProgram(); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3097 | setupDrawModelView(kModelViewMode_TranslateAndScale, false, |
| 3098 | x, y, x + texture->width, y + texture->height); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 3099 | setupDrawTexture(texture->id); |
| 3100 | setupDrawPureColorUniforms(); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3101 | setupDrawColorFilterUniforms(getColorFilter(paint)); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3102 | setupDrawShaderUniforms(getShader(paint)); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 3103 | setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 3104 | |
| 3105 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 3106 | } |
| 3107 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3108 | // Same values used by Skia |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3109 | #define kStdStrikeThru_Offset (-6.0f / 21.0f) |
| 3110 | #define kStdUnderline_Offset (1.0f / 9.0f) |
| 3111 | #define kStdUnderline_Thickness (1.0f / 18.0f) |
| 3112 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 3113 | void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y, |
| 3114 | const SkPaint* paint) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3115 | // Handle underline and strike-through |
| 3116 | uint32_t flags = paint->getFlags(); |
| 3117 | if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3118 | SkPaint paintCopy(*paint); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3119 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 3120 | if (CC_LIKELY(underlineWidth > 0.0f)) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3121 | const float textSize = paintCopy.getTextSize(); |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 3122 | const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3123 | |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 3124 | const float left = x; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3125 | float top = 0.0f; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3126 | |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 3127 | int linesCount = 0; |
| 3128 | if (flags & SkPaint::kUnderlineText_Flag) linesCount++; |
| 3129 | if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; |
| 3130 | |
| 3131 | const int pointsCount = 4 * linesCount; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3132 | float points[pointsCount]; |
| 3133 | int currentPoint = 0; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3134 | |
| 3135 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 3136 | top = y + textSize * kStdUnderline_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3137 | points[currentPoint++] = left; |
| 3138 | points[currentPoint++] = top; |
| 3139 | points[currentPoint++] = left + underlineWidth; |
| 3140 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 3144 | top = y + textSize * kStdStrikeThru_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3145 | points[currentPoint++] = left; |
| 3146 | points[currentPoint++] = top; |
| 3147 | points[currentPoint++] = left + underlineWidth; |
| 3148 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3149 | } |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3150 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3151 | paintCopy.setStrokeWidth(strokeWidth); |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3152 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3153 | drawLines(&points[0], pointsCount, &paintCopy); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3154 | } |
| 3155 | } |
| 3156 | } |
| 3157 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3158 | void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 3159 | if (mState.currentlyIgnored()) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3160 | return; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3161 | } |
| 3162 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3163 | drawColorRects(rects, count, paint, false, true, true); |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 3164 | } |
| 3165 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3166 | void OpenGLRenderer::drawShadow(float casterAlpha, |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 3167 | const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) { |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 3168 | if (mState.currentlyIgnored()) return; |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 3169 | |
Chris Craik | 15a07a2 | 2014-01-26 13:43:53 -0800 | [diff] [blame] | 3170 | // TODO: use quickRejectWithScissor. For now, always force enable scissor. |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 3171 | mCaches.enableScissor(); |
| 3172 | |
| 3173 | SkPaint paint; |
Chris Craik | ba9b613 | 2013-12-15 17:10:19 -0800 | [diff] [blame] | 3174 | paint.setAntiAlias(true); // want to use AlphaVertex |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 3175 | |
ztenghui | 14a4e35 | 2014-08-13 10:44:39 -0700 | [diff] [blame] | 3176 | // The caller has made sure casterAlpha > 0. |
| 3177 | float ambientShadowAlpha = mAmbientShadowAlpha; |
| 3178 | if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) { |
| 3179 | ambientShadowAlpha = mCaches.propertyAmbientShadowStrength; |
| 3180 | } |
| 3181 | if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) { |
| 3182 | paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0); |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 3183 | drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp); |
ztenghui | ef94c6f | 2014-02-13 17:09:45 -0800 | [diff] [blame] | 3184 | } |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 3185 | |
ztenghui | 14a4e35 | 2014-08-13 10:44:39 -0700 | [diff] [blame] | 3186 | float spotShadowAlpha = mSpotShadowAlpha; |
| 3187 | if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) { |
| 3188 | spotShadowAlpha = mCaches.propertySpotShadowStrength; |
| 3189 | } |
| 3190 | if (spotShadowVertexBuffer && spotShadowAlpha > 0) { |
| 3191 | paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0); |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 3192 | drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp); |
ztenghui | ef94c6f | 2014-02-13 17:09:45 -0800 | [diff] [blame] | 3193 | } |
ztenghui | 7b4516e | 2014-01-07 10:42:55 -0800 | [diff] [blame] | 3194 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3195 | mDirty=true; |
Chris Craik | f57776b | 2013-10-25 18:30:17 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3198 | void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3199 | bool ignoreTransform, bool dirty, bool clip) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3200 | if (count == 0) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3201 | return; |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3202 | } |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 3203 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3204 | int color = paint->getColor(); |
| 3205 | // If a shader is set, preserve only the alpha |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3206 | if (getShader(paint)) { |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3207 | color |= 0x00ffffff; |
| 3208 | } |
| 3209 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3210 | float left = FLT_MAX; |
| 3211 | float top = FLT_MAX; |
| 3212 | float right = FLT_MIN; |
| 3213 | float bottom = FLT_MIN; |
| 3214 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 3215 | Vertex mesh[count]; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3216 | Vertex* vertex = mesh; |
| 3217 | |
Chris Craik | 2af4635 | 2012-11-26 18:30:17 -0800 | [diff] [blame] | 3218 | for (int index = 0; index < count; index += 4) { |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3219 | float l = rects[index + 0]; |
| 3220 | float t = rects[index + 1]; |
| 3221 | float r = rects[index + 2]; |
| 3222 | float b = rects[index + 3]; |
| 3223 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3224 | Vertex::set(vertex++, l, t); |
| 3225 | Vertex::set(vertex++, r, t); |
| 3226 | Vertex::set(vertex++, l, b); |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3227 | Vertex::set(vertex++, r, b); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3228 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3229 | left = fminf(left, l); |
| 3230 | top = fminf(top, t); |
| 3231 | right = fmaxf(right, r); |
| 3232 | bottom = fmaxf(bottom, b); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3233 | } |
| 3234 | |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 3235 | if (clip && quickRejectSetupScissor(left, top, right, bottom)) { |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3236 | return; |
Romain Guy | a362c69 | 2013-02-04 13:50:16 -0800 | [diff] [blame] | 3237 | } |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3238 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3239 | setupDraw(); |
| 3240 | setupDrawNoTexture(); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 3241 | setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3242 | setupDrawShader(getShader(paint)); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3243 | setupDrawColorFilter(getColorFilter(paint)); |
| 3244 | setupDrawBlending(paint); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3245 | setupDrawProgram(); |
| 3246 | setupDrawDirtyRegionsDisabled(); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3247 | setupDrawModelView(kModelViewMode_Translate, false, |
| 3248 | 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3249 | setupDrawColorUniforms(getShader(paint)); |
| 3250 | setupDrawShaderUniforms(getShader(paint)); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3251 | setupDrawColorFilterUniforms(getColorFilter(paint)); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3252 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 3253 | if (dirty && hasLayer()) { |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 3254 | dirtyLayer(left, top, right, bottom, *currentTransform()); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3255 | } |
| 3256 | |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3257 | issueIndexedQuadDraw(&mesh[0], count / 4); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3258 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 3259 | mDirty = true; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3260 | } |
| 3261 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 3262 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3263 | const SkPaint* paint, bool ignoreTransform) { |
| 3264 | int color = paint->getColor(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3265 | // If a shader is set, preserve only the alpha |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3266 | if (getShader(paint)) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3267 | color |= 0x00ffffff; |
| 3268 | } |
| 3269 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3270 | setupDraw(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 3271 | setupDrawNoTexture(); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 3272 | setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3273 | setupDrawShader(getShader(paint)); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3274 | setupDrawColorFilter(getColorFilter(paint)); |
| 3275 | setupDrawBlending(paint); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3276 | setupDrawProgram(); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3277 | setupDrawModelView(kModelViewMode_TranslateAndScale, false, |
| 3278 | left, top, right, bottom, ignoreTransform); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3279 | setupDrawColorUniforms(getShader(paint)); |
| 3280 | setupDrawShaderUniforms(getShader(paint), ignoreTransform); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3281 | setupDrawColorFilterUniforms(getColorFilter(paint)); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3282 | setupDrawSimpleMesh(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 3283 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 3284 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 3285 | } |
| 3286 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3287 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 3288 | Texture* texture, const SkPaint* paint) { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3289 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 3290 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 3291 | GLvoid* vertices = (GLvoid*) nullptr; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3292 | GLvoid* texCoords = (GLvoid*) gMeshTextureOffset; |
| 3293 | |
| 3294 | if (texture->uvMapper) { |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 3295 | vertices = &mMeshVertices[0].x; |
| 3296 | texCoords = &mMeshVertices[0].u; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3297 | |
| 3298 | Rect uvs(0.0f, 0.0f, 1.0f, 1.0f); |
| 3299 | texture->uvMapper->map(uvs); |
| 3300 | |
| 3301 | resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom); |
| 3302 | } |
| 3303 | |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 3304 | if (CC_LIKELY(currentTransform()->isPureTranslate())) { |
| 3305 | const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f); |
| 3306 | const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3307 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3308 | texture->setFilter(GL_NEAREST, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3309 | drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3310 | paint, texture->blend, vertices, texCoords, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3311 | GL_TRIANGLE_STRIP, gMeshCount, false, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3312 | } else { |
Chris Craik | 67862524 | 2014-02-28 12:26:34 -0800 | [diff] [blame] | 3313 | texture->setFilter(getFilter(paint), true); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3314 | drawTextureMesh(left, top, right, bottom, texture->id, paint, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3315 | texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount); |
| 3316 | } |
| 3317 | |
| 3318 | if (texture->uvMapper) { |
| 3319 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3320 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 3323 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3324 | GLuint texture, const SkPaint* paint, bool blend, |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 3325 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3326 | bool swapSrcDst, bool ignoreTransform, GLuint vbo, |
| 3327 | ModelViewMode modelViewMode, bool dirty) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3328 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3329 | int a; |
| 3330 | SkXfermode::Mode mode; |
| 3331 | getAlphaAndMode(paint, &a, &mode); |
| 3332 | const float alpha = a / 255.0f; |
| 3333 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 3334 | setupDraw(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3335 | setupDrawWithTexture(); |
| 3336 | setupDrawColor(alpha, alpha, alpha, alpha); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3337 | setupDrawColorFilter(getColorFilter(paint)); |
| 3338 | setupDrawBlending(paint, blend, swapSrcDst); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3339 | setupDrawProgram(); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3340 | if (!dirty) setupDrawDirtyRegionsDisabled(); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3341 | setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3342 | setupDrawTexture(texture); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 3343 | setupDrawPureColorUniforms(); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3344 | setupDrawColorFilterUniforms(getColorFilter(paint)); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3345 | setupDrawMesh(vertices, texCoords, vbo); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 3346 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 3347 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3350 | void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3351 | GLuint texture, const SkPaint* paint, bool blend, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3352 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3353 | bool swapSrcDst, bool ignoreTransform, GLuint vbo, |
| 3354 | ModelViewMode modelViewMode, bool dirty) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3355 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3356 | int a; |
| 3357 | SkXfermode::Mode mode; |
| 3358 | getAlphaAndMode(paint, &a, &mode); |
| 3359 | const float alpha = a / 255.0f; |
| 3360 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3361 | setupDraw(); |
| 3362 | setupDrawWithTexture(); |
| 3363 | setupDrawColor(alpha, alpha, alpha, alpha); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3364 | setupDrawColorFilter(getColorFilter(paint)); |
| 3365 | setupDrawBlending(paint, blend, swapSrcDst); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3366 | setupDrawProgram(); |
| 3367 | if (!dirty) setupDrawDirtyRegionsDisabled(); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3368 | setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3369 | setupDrawTexture(texture); |
| 3370 | setupDrawPureColorUniforms(); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3371 | setupDrawColorFilterUniforms(getColorFilter(paint)); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3372 | setupDrawMeshIndices(vertices, texCoords, vbo); |
| 3373 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 3374 | glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3377 | void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom, |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3378 | GLuint texture, const SkPaint* paint, |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3379 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3380 | bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) { |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3381 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 3382 | int color = paint != nullptr ? paint->getColor() : 0; |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3383 | int alpha; |
| 3384 | SkXfermode::Mode mode; |
| 3385 | getAlphaAndMode(paint, &alpha, &mode); |
| 3386 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3387 | setupDraw(); |
| 3388 | setupDrawWithTexture(true); |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 3389 | if (paint != nullptr) { |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3390 | setupDrawAlpha8Color(color, alpha); |
| 3391 | } |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3392 | setupDrawColorFilter(getColorFilter(paint)); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3393 | setupDrawShader(getShader(paint)); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3394 | setupDrawBlending(paint, true); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3395 | setupDrawProgram(); |
| 3396 | if (!dirty) setupDrawDirtyRegionsDisabled(); |
Chris Craik | 4063a0e | 2013-11-15 16:06:56 -0800 | [diff] [blame] | 3397 | setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3398 | setupDrawTexture(texture); |
| 3399 | setupDrawPureColorUniforms(); |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3400 | setupDrawColorFilterUniforms(getColorFilter(paint)); |
Leon Scroggins III | d1ad5e6 | 2014-05-05 12:50:38 -0400 | [diff] [blame] | 3401 | setupDrawShaderUniforms(getShader(paint), ignoreTransform); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3402 | setupDrawMesh(vertices, texCoords); |
| 3403 | |
| 3404 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3405 | } |
| 3406 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3407 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3408 | ProgramDescription& description, bool swapSrcDst) { |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 3409 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 3410 | if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) { |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 3411 | blend = true; |
| 3412 | mDescription.hasRoundRectClip = true; |
| 3413 | } |
| 3414 | mSkipOutlineClip = true; |
| 3415 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3416 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
Romain Guy | c189ef5 | 2012-04-25 20:02:53 -0700 | [diff] [blame] | 3417 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3418 | if (blend) { |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 3419 | // These blend modes are not supported by OpenGL directly and have |
| 3420 | // to be implemented using shaders. Since the shader will perform |
| 3421 | // the blending, turn blending off here |
| 3422 | // If the blend mode cannot be implemented using shaders, fall |
| 3423 | // back to the default SrcOver blend mode instead |
Romain Guy | 33fa1f7 | 2012-08-07 19:09:57 -0700 | [diff] [blame] | 3424 | if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 3425 | if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3426 | description.framebufferMode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3427 | description.swapSrcDst = swapSrcDst; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3428 | |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 3429 | if (mCaches.blend) { |
| 3430 | glDisable(GL_BLEND); |
| 3431 | mCaches.blend = false; |
| 3432 | } |
| 3433 | |
| 3434 | return; |
| 3435 | } else { |
| 3436 | mode = SkXfermode::kSrcOver_Mode; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3437 | } |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | if (!mCaches.blend) { |
| 3441 | glEnable(GL_BLEND); |
| 3442 | } |
| 3443 | |
| 3444 | GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; |
| 3445 | GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; |
| 3446 | |
| 3447 | if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { |
| 3448 | glBlendFunc(sourceMode, destMode); |
| 3449 | mCaches.lastSrcMode = sourceMode; |
| 3450 | mCaches.lastDstMode = destMode; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3451 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3452 | } else if (mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3453 | glDisable(GL_BLEND); |
| 3454 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3455 | mCaches.blend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 3456 | } |
| 3457 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 3458 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3459 | if (!program->isInUse()) { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 3460 | if (mCaches.currentProgram != nullptr) mCaches.currentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3461 | program->use(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3462 | mCaches.currentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3463 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 3464 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3465 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 3466 | } |
| 3467 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 3468 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 3469 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3470 | TextureVertex::setUV(v++, u1, v1); |
| 3471 | TextureVertex::setUV(v++, u2, v1); |
| 3472 | TextureVertex::setUV(v++, u1, v2); |
| 3473 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 3474 | } |
| 3475 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame^] | 3476 | void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, |
| 3477 | SkXfermode::Mode* mode) const { |
Romain Guy | be6f9dc | 2012-07-16 12:41:17 -0700 | [diff] [blame] | 3478 | getAlphaAndModeDirect(paint, alpha, mode); |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 3479 | if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) { |
| 3480 | // if drawing a layer, ignore the paint's alpha |
Romain Guy | 87b515c | 2013-05-03 17:42:27 -0700 | [diff] [blame] | 3481 | *alpha = mDrawModifiers.mOverrideLayerAlpha * 255; |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 3482 | } |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 3483 | *alpha *= currentSnapshot()->alpha; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 3484 | } |
| 3485 | |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 3486 | float OpenGLRenderer::getLayerAlpha(const Layer* layer) const { |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 3487 | float alpha; |
| 3488 | if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) { |
| 3489 | alpha = mDrawModifiers.mOverrideLayerAlpha; |
| 3490 | } else { |
| 3491 | alpha = layer->getAlpha() / 255.0f; |
| 3492 | } |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 3493 | return alpha * currentSnapshot()->alpha; |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 3494 | } |
| 3495 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 3496 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 3497 | }; // namespace android |