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