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