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