John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 5 | * you mPrimitiveFields.may not use this file except in compliance with the License. |
| 6 | * You mPrimitiveFields.may obtain a copy of the License at |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 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 | */ |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 16 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 19 | #include "RenderProperties.h" |
| 20 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 21 | #include <utils/Trace.h> |
| 22 | |
| 23 | #include <SkCanvas.h> |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 24 | #include <SkColorFilter.h> |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 25 | #include <SkMatrix.h> |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 26 | #include <SkPath.h> |
| 27 | #include <SkPathOps.h> |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 28 | |
| 29 | #include "Matrix.h" |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 30 | #include "OpenGLRenderer.h" |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 31 | #include "utils/MathUtils.h" |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 32 | |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 36 | LayerProperties::LayerProperties() { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 37 | reset(); |
| 38 | } |
| 39 | |
| 40 | LayerProperties::~LayerProperties() { |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 41 | setType(LayerType::None); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void LayerProperties::reset() { |
| 45 | mOpaque = false; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 46 | setFromPaint(nullptr); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool LayerProperties::setColorFilter(SkColorFilter* filter) { |
| 50 | if (mColorFilter == filter) return false; |
| 51 | SkRefCnt_SafeAssign(mColorFilter, filter); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | bool LayerProperties::setFromPaint(const SkPaint* paint) { |
| 56 | bool changed = false; |
| 57 | SkXfermode::Mode mode; |
| 58 | int alpha; |
| 59 | OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode); |
| 60 | changed |= setAlpha(static_cast<uint8_t>(alpha)); |
| 61 | changed |= setXferMode(mode); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 62 | changed |= setColorFilter(paint ? paint->getColorFilter() : nullptr); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 63 | return changed; |
| 64 | } |
| 65 | |
| 66 | LayerProperties& LayerProperties::operator=(const LayerProperties& other) { |
| 67 | setType(other.type()); |
| 68 | setOpaque(other.opaque()); |
| 69 | setAlpha(other.alpha()); |
| 70 | setXferMode(other.xferMode()); |
| 71 | setColorFilter(other.colorFilter()); |
| 72 | return *this; |
| 73 | } |
| 74 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 75 | RenderProperties::PrimitiveFields::PrimitiveFields() |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 76 | : mClippingFlags(CLIP_TO_BOUNDS) |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 77 | , mProjectBackwards(false) |
| 78 | , mProjectionReceiver(false) |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 79 | , mAlpha(1) |
| 80 | , mHasOverlappingRendering(true) |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 81 | , mElevation(0) |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 82 | , mTranslationX(0), mTranslationY(0), mTranslationZ(0) |
| 83 | , mRotation(0), mRotationX(0), mRotationY(0) |
| 84 | , mScaleX(1), mScaleY(1) |
| 85 | , mPivotX(0), mPivotY(0) |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 86 | , mLeft(0), mTop(0), mRight(0), mBottom(0) |
| 87 | , mWidth(0), mHeight(0) |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 88 | , mPivotExplicitlySet(false) |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 89 | , mMatrixOrPivotDirty(false) { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 90 | } |
| 91 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 92 | RenderProperties::ComputedFields::ComputedFields() |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 93 | : mTransformMatrix(nullptr) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | RenderProperties::ComputedFields::~ComputedFields() { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 97 | delete mTransformMatrix; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | RenderProperties::RenderProperties() |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 101 | : mStaticMatrix(nullptr) |
| 102 | , mAnimationMatrix(nullptr) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | RenderProperties::~RenderProperties() { |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 106 | delete mStaticMatrix; |
| 107 | delete mAnimationMatrix; |
| 108 | } |
| 109 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 110 | RenderProperties& RenderProperties::operator=(const RenderProperties& other) { |
| 111 | if (this != &other) { |
| 112 | mPrimitiveFields = other.mPrimitiveFields; |
| 113 | setStaticMatrix(other.getStaticMatrix()); |
| 114 | setAnimationMatrix(other.getAnimationMatrix()); |
| 115 | setCameraDistance(other.getCameraDistance()); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 116 | mLayerProperties = other.layerProperties(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 117 | |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 118 | // Force recalculation of the matrix, since other's dirty bit may be clear |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 119 | mPrimitiveFields.mMatrixOrPivotDirty = true; |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 120 | updateMatrix(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 121 | } |
| 122 | return *this; |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 123 | } |
| 124 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 125 | void RenderProperties::debugOutputProperties(const int level) const { |
| 126 | if (mPrimitiveFields.mLeft != 0 || mPrimitiveFields.mTop != 0) { |
| 127 | ALOGD("%*sTranslate (left, top) %d, %d", level * 2, "", mPrimitiveFields.mLeft, mPrimitiveFields.mTop); |
| 128 | } |
| 129 | if (mStaticMatrix) { |
| 130 | ALOGD("%*sConcatMatrix (static) %p: " SK_MATRIX_STRING, |
| 131 | level * 2, "", mStaticMatrix, SK_MATRIX_ARGS(mStaticMatrix)); |
| 132 | } |
| 133 | if (mAnimationMatrix) { |
| 134 | ALOGD("%*sConcatMatrix (animation) %p: " SK_MATRIX_STRING, |
| 135 | level * 2, "", mAnimationMatrix, SK_MATRIX_ARGS(mAnimationMatrix)); |
| 136 | } |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 137 | if (hasTransformMatrix()) { |
| 138 | if (isTransformTranslateOnly()) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 139 | ALOGD("%*sTranslate %.2f, %.2f, %.2f", |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 140 | level * 2, "", getTranslationX(), getTranslationY(), getZ()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 141 | } else { |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 142 | ALOGD("%*sConcatMatrix %p: " SK_MATRIX_STRING, |
| 143 | level * 2, "", mComputedFields.mTransformMatrix, SK_MATRIX_ARGS(mComputedFields.mTransformMatrix)); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Chris Craik | 856f0cc | 2015-04-21 15:13:29 -0700 | [diff] [blame] | 147 | const bool isLayer = effectiveLayerType() != LayerType::None; |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 148 | int clipFlags = getClippingFlags(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 149 | if (mPrimitiveFields.mAlpha < 1) { |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 150 | if (isLayer) { |
| 151 | clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 152 | } |
Chris Craik | 8df5ffa | 2015-04-28 17:47:20 -0700 | [diff] [blame] | 153 | |
| 154 | LOG_ALWAYS_FATAL_IF(!isLayer && mPrimitiveFields.mHasOverlappingRendering); |
| 155 | ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 156 | } |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 157 | if (clipFlags) { |
| 158 | Rect clipRect; |
| 159 | getClippingRectForFlags(clipFlags, &clipRect); |
John Reck | 78ce1c5 | 2014-03-24 15:43:49 -0700 | [diff] [blame] | 160 | ALOGD("%*sClipRect %d, %d, %d, %d", level * 2, "", |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 161 | (int)clipRect.left, (int)clipRect.top, (int)clipRect.right, (int)clipRect.bottom); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 162 | } |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void RenderProperties::updateMatrix() { |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 166 | if (mPrimitiveFields.mMatrixOrPivotDirty) { |
| 167 | if (!mComputedFields.mTransformMatrix) { |
| 168 | // only allocate a mPrimitiveFields.matrix if we have a complex transform |
| 169 | mComputedFields.mTransformMatrix = new SkMatrix(); |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 170 | } |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 171 | if (!mPrimitiveFields.mPivotExplicitlySet) { |
| 172 | mPrimitiveFields.mPivotX = mPrimitiveFields.mWidth / 2.0f; |
| 173 | mPrimitiveFields.mPivotY = mPrimitiveFields.mHeight / 2.0f; |
| 174 | } |
| 175 | SkMatrix* transform = mComputedFields.mTransformMatrix; |
| 176 | transform->reset(); |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 177 | if (MathUtils::isZero(getRotationX()) && MathUtils::isZero(getRotationY())) { |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 178 | transform->setTranslate(getTranslationX(), getTranslationY()); |
| 179 | transform->preRotate(getRotation(), getPivotX(), getPivotY()); |
| 180 | transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY()); |
| 181 | } else { |
| 182 | SkMatrix transform3D; |
| 183 | mComputedFields.mTransformCamera.save(); |
| 184 | transform->preScale(getScaleX(), getScaleY(), getPivotX(), getPivotY()); |
| 185 | mComputedFields.mTransformCamera.rotateX(mPrimitiveFields.mRotationX); |
| 186 | mComputedFields.mTransformCamera.rotateY(mPrimitiveFields.mRotationY); |
| 187 | mComputedFields.mTransformCamera.rotateZ(-mPrimitiveFields.mRotation); |
| 188 | mComputedFields.mTransformCamera.getMatrix(&transform3D); |
| 189 | transform3D.preTranslate(-getPivotX(), -getPivotY()); |
| 190 | transform3D.postTranslate(getPivotX() + getTranslationX(), |
| 191 | getPivotY() + getTranslationY()); |
| 192 | transform->postConcat(transform3D); |
| 193 | mComputedFields.mTransformCamera.restore(); |
| 194 | } |
| 195 | mPrimitiveFields.mMatrixOrPivotDirty = false; |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
John Reck | acb6f07 | 2014-03-12 16:11:23 -0700 | [diff] [blame] | 199 | } /* namespace uirenderer */ |
| 200 | } /* namespace android */ |