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