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