Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 17 | #include "SkiaCanvas.h" |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 18 | |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 19 | #include "CanvasProperty.h" |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 20 | #include "NinePatchUtils.h" |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 21 | #include "VectorDrawable.h" |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 22 | #include "hwui/Bitmap.h" |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 23 | #include "hwui/MinikinUtils.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 24 | #include "pipeline/skia/AnimatedDrawables.h" |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 25 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 26 | #include <SkDrawable.h> |
John Reck | 849911a | 2015-01-20 07:51:14 -0800 | [diff] [blame] | 27 | #include <SkDeque.h> |
| 28 | #include <SkDrawFilter.h> |
| 29 | #include <SkGraphics.h> |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 30 | #include <SkImage.h> |
Matt Sarett | 62feb3a | 2016-09-20 10:34:11 -0400 | [diff] [blame] | 31 | #include <SkImagePriv.h> |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 32 | #include <SkRSXform.h> |
John Reck | 849911a | 2015-01-20 07:51:14 -0800 | [diff] [blame] | 33 | #include <SkShader.h> |
John Reck | 849911a | 2015-01-20 07:51:14 -0800 | [diff] [blame] | 34 | #include <SkTemplates.h> |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 35 | #include <SkTextBlob.h> |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 36 | |
Ben Wagner | 60126ef | 2015-08-07 12:13:48 -0400 | [diff] [blame] | 37 | #include <memory> |
| 38 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 41 | using uirenderer::PaintUtils; |
| 42 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 43 | Canvas* Canvas::create_canvas(const SkBitmap& bitmap) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 44 | return new SkiaCanvas(bitmap); |
| 45 | } |
| 46 | |
| 47 | Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) { |
| 48 | return new SkiaCanvas(skiaCanvas); |
| 49 | } |
| 50 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 51 | SkiaCanvas::SkiaCanvas() {} |
| 52 | |
| 53 | SkiaCanvas::SkiaCanvas(SkCanvas* canvas) |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 54 | : mCanvas(canvas) {} |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 55 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 56 | SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) { |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 57 | mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap)); |
| 58 | mCanvas = mCanvasOwned.get(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 61 | SkiaCanvas::~SkiaCanvas() {} |
| 62 | |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 63 | void SkiaCanvas::reset(SkCanvas* skiaCanvas) { |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 64 | if (mCanvas != skiaCanvas) { |
| 65 | mCanvas = skiaCanvas; |
| 66 | mCanvasOwned.reset(); |
| 67 | } |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 68 | mSaveStack.reset(nullptr); |
| 69 | mHighContrastText = false; |
| 70 | } |
| 71 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 72 | // ---------------------------------------------------------------------------- |
| 73 | // Canvas state operations: Replace Bitmap |
| 74 | // ---------------------------------------------------------------------------- |
| 75 | |
Tony Mantler | 4f641d1 | 2017-03-14 22:36:14 +0000 | [diff] [blame] | 76 | class ClipCopier : public SkCanvas::ClipVisitor { |
| 77 | public: |
| 78 | explicit ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {} |
| 79 | |
| 80 | virtual void clipRect(const SkRect& rect, SkClipOp op, bool antialias) { |
| 81 | m_dstCanvas->clipRect(rect, op, antialias); |
| 82 | } |
| 83 | virtual void clipRRect(const SkRRect& rrect, SkClipOp op, bool antialias) { |
| 84 | m_dstCanvas->clipRRect(rrect, op, antialias); |
| 85 | } |
| 86 | virtual void clipPath(const SkPath& path, SkClipOp op, bool antialias) { |
| 87 | m_dstCanvas->clipPath(path, op, antialias); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | SkCanvas* m_dstCanvas; |
| 92 | }; |
| 93 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 94 | void SkiaCanvas::setBitmap(const SkBitmap& bitmap) { |
Tony Mantler | 4f641d1 | 2017-03-14 22:36:14 +0000 | [diff] [blame] | 95 | SkCanvas* newCanvas = new SkCanvas(bitmap); |
| 96 | |
| 97 | if (!bitmap.isNull()) { |
| 98 | // Copy the canvas matrix & clip state. |
| 99 | newCanvas->setMatrix(mCanvas->getTotalMatrix()); |
| 100 | |
| 101 | ClipCopier copier(newCanvas); |
| 102 | mCanvas->replayClips(&copier); |
| 103 | } |
| 104 | |
| 105 | // deletes the previously owned canvas (if any) |
| 106 | mCanvasOwned = std::unique_ptr<SkCanvas>(newCanvas); |
| 107 | mCanvas = newCanvas; |
| 108 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 109 | // clean up the old save stack |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 110 | mSaveStack.reset(nullptr); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // ---------------------------------------------------------------------------- |
| 114 | // Canvas state operations |
| 115 | // ---------------------------------------------------------------------------- |
| 116 | |
| 117 | bool SkiaCanvas::isOpaque() { |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 118 | return mCanvas->imageInfo().isOpaque(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int SkiaCanvas::width() { |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 122 | return mCanvas->imageInfo().width(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | int SkiaCanvas::height() { |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 126 | return mCanvas->imageInfo().height(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // ---------------------------------------------------------------------------- |
| 130 | // Canvas state operations: Save (layer) |
| 131 | // ---------------------------------------------------------------------------- |
| 132 | |
| 133 | int SkiaCanvas::getSaveCount() const { |
| 134 | return mCanvas->getSaveCount(); |
| 135 | } |
| 136 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 137 | int SkiaCanvas::save(SaveFlags::Flags flags) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 138 | int count = mCanvas->save(); |
| 139 | recordPartialSave(flags); |
| 140 | return count; |
| 141 | } |
| 142 | |
Florin Malita | 5e27140 | 2015-11-04 14:36:02 -0500 | [diff] [blame] | 143 | // The SkiaCanvas::restore operation layers on the capability to preserve |
| 144 | // either (or both) the matrix and/or clip state after a SkCanvas::restore |
| 145 | // operation. It does this by explicitly saving off the clip & matrix state |
| 146 | // when requested and playing it back after the SkCanvas::restore. |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 147 | void SkiaCanvas::restore() { |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 148 | const auto* rec = this->currentSaveRec(); |
| 149 | if (!rec) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 150 | // Fast path - no record for this frame. |
| 151 | mCanvas->restore(); |
| 152 | return; |
| 153 | } |
| 154 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 155 | bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix); |
| 156 | bool preserveClip = !(rec->saveFlags & SaveFlags::Clip); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 157 | |
| 158 | SkMatrix savedMatrix; |
| 159 | if (preserveMatrix) { |
| 160 | savedMatrix = mCanvas->getTotalMatrix(); |
| 161 | } |
| 162 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 163 | const size_t clipIndex = rec->clipIndex; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 164 | |
| 165 | mCanvas->restore(); |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 166 | mSaveStack->pop_back(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 167 | |
| 168 | if (preserveMatrix) { |
| 169 | mCanvas->setMatrix(savedMatrix); |
| 170 | } |
| 171 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 172 | if (preserveClip) { |
| 173 | this->applyPersistentClips(clipIndex); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 174 | } |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void SkiaCanvas::restoreToCount(int restoreCount) { |
| 178 | while (mCanvas->getSaveCount() > restoreCount) { |
| 179 | this->restore(); |
| 180 | } |
| 181 | } |
| 182 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 183 | static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) { |
| 184 | SkCanvas::SaveLayerFlags layerFlags = 0; |
| 185 | |
Yuqian Li | 83427ff | 2016-09-14 11:14:06 -0400 | [diff] [blame] | 186 | // We intentionally ignore the SaveFlags::HasAlphaLayer and |
| 187 | // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it |
| 188 | // and our Android client may use it incorrectly. |
| 189 | // In Skia, this flag is purely for performance optimization. |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 190 | |
| 191 | if (!(flags & SaveFlags::ClipToLayer)) { |
| 192 | layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag; |
| 193 | } |
| 194 | |
| 195 | return layerFlags; |
| 196 | } |
| 197 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 198 | int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 199 | const SkPaint* paint, SaveFlags::Flags flags) { |
| 200 | const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom); |
Derek Sollenberger | b820119 | 2017-01-09 16:11:59 -0500 | [diff] [blame] | 201 | const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags)); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 202 | |
Stan Iliev | 68885e3 | 2016-12-14 11:18:34 -0500 | [diff] [blame] | 203 | return mCanvas->saveLayer(rec); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 207 | int alpha, SaveFlags::Flags flags) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 208 | if (static_cast<unsigned>(alpha) < 0xFF) { |
Yuqian Li | fd92ee4 | 2016-04-27 17:03:38 -0400 | [diff] [blame] | 209 | SkPaint alphaPaint; |
| 210 | alphaPaint.setAlpha(alpha); |
| 211 | return this->saveLayer(left, top, right, bottom, &alphaPaint, flags); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 212 | } |
Yuqian Li | fd92ee4 | 2016-04-27 17:03:38 -0400 | [diff] [blame] | 213 | return this->saveLayer(left, top, right, bottom, nullptr, flags); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 216 | class SkiaCanvas::Clip { |
| 217 | public: |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 218 | Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m) |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 219 | : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {} |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 220 | Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m) |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 221 | : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {} |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 222 | Clip(const SkPath& path, SkClipOp op, const SkMatrix& m) |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 223 | : mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {} |
| 224 | |
| 225 | void apply(SkCanvas* canvas) const { |
| 226 | canvas->setMatrix(mMatrix); |
| 227 | switch (mType) { |
| 228 | case Type::Rect: |
| 229 | canvas->clipRect(mRRect.rect(), mOp); |
| 230 | break; |
| 231 | case Type::RRect: |
| 232 | canvas->clipRRect(mRRect, mOp); |
| 233 | break; |
| 234 | case Type::Path: |
| 235 | canvas->clipPath(*mPath.get(), mOp); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | private: |
| 241 | enum class Type { |
| 242 | Rect, |
| 243 | RRect, |
| 244 | Path, |
| 245 | }; |
| 246 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 247 | Type mType; |
| 248 | SkClipOp mOp; |
| 249 | SkMatrix mMatrix; |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 250 | |
| 251 | // These are logically a union (tracked separately due to non-POD path). |
| 252 | SkTLazy<SkPath> mPath; |
| 253 | SkRRect mRRect; |
| 254 | }; |
| 255 | |
| 256 | const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const { |
| 257 | const SaveRec* rec = mSaveStack |
| 258 | ? static_cast<const SaveRec*>(mSaveStack->back()) |
| 259 | : nullptr; |
| 260 | int currentSaveCount = mCanvas->getSaveCount(); |
| 261 | SkASSERT(!rec || currentSaveCount >= rec->saveCount); |
| 262 | |
| 263 | return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr; |
| 264 | } |
| 265 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 266 | // ---------------------------------------------------------------------------- |
| 267 | // functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags) |
| 268 | // ---------------------------------------------------------------------------- |
| 269 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 270 | void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 271 | // A partial save is a save operation which doesn't capture the full canvas state. |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 272 | // (either SaveFlags::Matrix or SaveFlags::Clip is missing). |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 273 | |
| 274 | // Mask-out non canvas state bits. |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 275 | flags &= SaveFlags::MatrixClip; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 276 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 277 | if (flags == SaveFlags::MatrixClip) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 278 | // not a partial save. |
| 279 | return; |
| 280 | } |
| 281 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 282 | if (!mSaveStack) { |
Ben Wagner | d1cbc16 | 2015-08-19 12:45:09 -0400 | [diff] [blame] | 283 | mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8)); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back()); |
Florin Malita | 5e27140 | 2015-11-04 14:36:02 -0500 | [diff] [blame] | 287 | rec->saveCount = mCanvas->getSaveCount(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 288 | rec->saveFlags = flags; |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 289 | rec->clipIndex = mClipStack.size(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 292 | template <typename T> |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 293 | void SkiaCanvas::recordClip(const T& clip, SkClipOp op) { |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 294 | // Only need tracking when in a partial save frame which |
| 295 | // doesn't restore the clip. |
| 296 | const SaveRec* rec = this->currentSaveRec(); |
| 297 | if (rec && !(rec->saveFlags & SaveFlags::Clip)) { |
| 298 | mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix()); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 302 | // Applies and optionally removes all clips >= index. |
| 303 | void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) { |
| 304 | SkASSERT(clipStartIndex <= mClipStack.size()); |
| 305 | const auto begin = mClipStack.cbegin() + clipStartIndex; |
| 306 | const auto end = mClipStack.cend(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 307 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 308 | // Clip application mutates the CTM. |
| 309 | const SkMatrix saveMatrix = mCanvas->getTotalMatrix(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 310 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 311 | for (auto clip = begin; clip != end; ++clip) { |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 312 | clip->apply(mCanvas); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 315 | mCanvas->setMatrix(saveMatrix); |
| 316 | |
| 317 | // If the current/post-restore save rec is also persisting clips, we |
| 318 | // leave them on the stack to be reapplied part of the next restore(). |
| 319 | // Otherwise we're done and just pop them. |
| 320 | const auto* rec = this->currentSaveRec(); |
| 321 | if (!rec || (rec->saveFlags & SaveFlags::Clip)) { |
| 322 | mClipStack.erase(begin, end); |
| 323 | } |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // ---------------------------------------------------------------------------- |
| 327 | // Canvas state operations: Matrix |
| 328 | // ---------------------------------------------------------------------------- |
| 329 | |
| 330 | void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const { |
| 331 | *outMatrix = mCanvas->getTotalMatrix(); |
| 332 | } |
| 333 | |
| 334 | void SkiaCanvas::setMatrix(const SkMatrix& matrix) { |
| 335 | mCanvas->setMatrix(matrix); |
| 336 | } |
| 337 | |
| 338 | void SkiaCanvas::concat(const SkMatrix& matrix) { |
| 339 | mCanvas->concat(matrix); |
| 340 | } |
| 341 | |
| 342 | void SkiaCanvas::rotate(float degrees) { |
| 343 | mCanvas->rotate(degrees); |
| 344 | } |
| 345 | |
| 346 | void SkiaCanvas::scale(float sx, float sy) { |
| 347 | mCanvas->scale(sx, sy); |
| 348 | } |
| 349 | |
| 350 | void SkiaCanvas::skew(float sx, float sy) { |
| 351 | mCanvas->skew(sx, sy); |
| 352 | } |
| 353 | |
| 354 | void SkiaCanvas::translate(float dx, float dy) { |
| 355 | mCanvas->translate(dx, dy); |
| 356 | } |
| 357 | |
| 358 | // ---------------------------------------------------------------------------- |
| 359 | // Canvas state operations: Clips |
| 360 | // ---------------------------------------------------------------------------- |
| 361 | |
| 362 | // This function is a mirror of SkCanvas::getClipBounds except that it does |
| 363 | // not outset the edge of the clip to account for anti-aliasing. There is |
| 364 | // a skia bug to investigate pushing this logic into back into skia. |
| 365 | // (see https://code.google.com/p/skia/issues/detail?id=1303) |
| 366 | bool SkiaCanvas::getClipBounds(SkRect* outRect) const { |
| 367 | SkIRect ibounds; |
Mike Reed | 5e43898 | 2017-01-25 08:23:25 -0500 | [diff] [blame] | 368 | if (!mCanvas->getDeviceClipBounds(&ibounds)) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | |
| 372 | SkMatrix inverse; |
| 373 | // if we can't invert the CTM, we can't return local clip bounds |
| 374 | if (!mCanvas->getTotalMatrix().invert(&inverse)) { |
| 375 | if (outRect) { |
| 376 | outRect->setEmpty(); |
| 377 | } |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | if (NULL != outRect) { |
| 382 | SkRect r = SkRect::Make(ibounds); |
| 383 | inverse.mapRect(outRect, r); |
| 384 | } |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const { |
| 389 | SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom); |
| 390 | return mCanvas->quickReject(bounds); |
| 391 | } |
| 392 | |
| 393 | bool SkiaCanvas::quickRejectPath(const SkPath& path) const { |
| 394 | return mCanvas->quickReject(path); |
| 395 | } |
| 396 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 397 | bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 398 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 399 | this->recordClip(rect, op); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 400 | mCanvas->clipRect(rect, op); |
Chris Craik | 5ec6a28 | 2015-06-23 15:42:12 -0700 | [diff] [blame] | 401 | return !mCanvas->isClipEmpty(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 402 | } |
| 403 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 404 | bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) { |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 405 | SkRRect roundRect; |
| 406 | if (path->isRRect(&roundRect)) { |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 407 | this->recordClip(roundRect, op); |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 408 | mCanvas->clipRRect(roundRect, op); |
| 409 | } else { |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 410 | this->recordClip(*path, op); |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 411 | mCanvas->clipPath(*path, op); |
| 412 | } |
Chris Craik | 5ec6a28 | 2015-06-23 15:42:12 -0700 | [diff] [blame] | 413 | return !mCanvas->isClipEmpty(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 416 | // ---------------------------------------------------------------------------- |
| 417 | // Canvas state operations: Filters |
| 418 | // ---------------------------------------------------------------------------- |
| 419 | |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 420 | SkDrawFilter* SkiaCanvas::getDrawFilter() { |
| 421 | return mCanvas->getDrawFilter(); |
| 422 | } |
| 423 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 424 | void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) { |
| 425 | mCanvas->setDrawFilter(drawFilter); |
| 426 | } |
| 427 | |
| 428 | // ---------------------------------------------------------------------------- |
| 429 | // Canvas draw operations |
| 430 | // ---------------------------------------------------------------------------- |
| 431 | |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 432 | void SkiaCanvas::drawColor(int color, SkBlendMode mode) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 433 | mCanvas->drawColor(color, mode); |
| 434 | } |
| 435 | |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 436 | void SkiaCanvas::drawPaint(const SkPaint& paint) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 437 | mCanvas->drawPaint(paint); |
| 438 | } |
| 439 | |
| 440 | // ---------------------------------------------------------------------------- |
| 441 | // Canvas draw operations: Geometry |
| 442 | // ---------------------------------------------------------------------------- |
| 443 | |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 444 | void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 445 | SkCanvas::PointMode mode) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 446 | if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 447 | // convert the floats into SkPoints |
| 448 | count >>= 1; // now it is the number of points |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 449 | std::unique_ptr<SkPoint[]> pts(new SkPoint[count]); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 450 | for (int i = 0; i < count; i++) { |
| 451 | pts[i].set(points[0], points[1]); |
| 452 | points += 2; |
| 453 | } |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 454 | mCanvas->drawPoints(mode, count, pts.get(), paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 458 | void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 459 | mCanvas->drawPoint(x, y, paint); |
| 460 | } |
| 461 | |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 462 | void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 463 | this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode); |
| 464 | } |
| 465 | |
| 466 | void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY, |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 467 | const SkPaint& paint) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 468 | mCanvas->drawLine(startX, startY, stopX, stopY, paint); |
| 469 | } |
| 470 | |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 471 | void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 472 | if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 473 | this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode); |
| 474 | } |
| 475 | |
| 476 | void SkiaCanvas::drawRect(float left, float top, float right, float bottom, |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 477 | const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 478 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 479 | mCanvas->drawRectCoords(left, top, right, bottom, paint); |
| 480 | |
| 481 | } |
| 482 | |
Derek Sollenberger | 94394b3 | 2015-07-10 09:58:41 -0400 | [diff] [blame] | 483 | void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 484 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 485 | mCanvas->drawRegion(region, paint); |
Derek Sollenberger | 94394b3 | 2015-07-10 09:58:41 -0400 | [diff] [blame] | 486 | } |
| 487 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 488 | void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 489 | float rx, float ry, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 490 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 491 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
| 492 | mCanvas->drawRoundRect(rect, rx, ry, paint); |
| 493 | } |
| 494 | |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 495 | void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 496 | if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 497 | mCanvas->drawCircle(x, y, radius, paint); |
| 498 | } |
| 499 | |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 500 | void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 501 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 502 | SkRect oval = SkRect::MakeLTRB(left, top, right, bottom); |
| 503 | mCanvas->drawOval(oval, paint); |
| 504 | } |
| 505 | |
| 506 | void SkiaCanvas::drawArc(float left, float top, float right, float bottom, |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 507 | float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 508 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 509 | SkRect arc = SkRect::MakeLTRB(left, top, right, bottom); |
| 510 | mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint); |
| 511 | } |
| 512 | |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 513 | void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 514 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Derek Sollenberger | d7f13f8 | 2017-03-09 13:08:27 -0500 | [diff] [blame] | 515 | mCanvas->drawPath(path, paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount, |
| 519 | const float* verts, const float* texs, const int* colors, |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 520 | const uint16_t* indices, int indexCount, const SkPaint& paint) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 521 | #ifndef SK_SCALAR_IS_FLOAT |
| 522 | SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid"); |
| 523 | #endif |
| 524 | const int ptCount = vertexCount >> 1; |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame^] | 525 | mCanvas->drawVertices(SkVertices::MakeCopy(vertexMode, ptCount, (SkPoint*)verts, |
| 526 | (SkPoint*)texs, (SkColor*)colors, |
| 527 | indexCount, indices), |
| 528 | SkBlendMode::kModulate, paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // ---------------------------------------------------------------------------- |
| 532 | // Canvas draw operations: Bitmaps |
| 533 | // ---------------------------------------------------------------------------- |
| 534 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 535 | void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) { |
| 536 | SkBitmap skBitmap; |
| 537 | bitmap.getSkBitmap(&skBitmap); |
| 538 | mCanvas->drawBitmap(skBitmap, left, top, paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 539 | } |
| 540 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 541 | void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, const SkMatrix& matrix, const SkPaint* paint) { |
| 542 | SkBitmap bitmap; |
| 543 | hwuiBitmap.getSkBitmap(&bitmap); |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 544 | SkAutoCanvasRestore acr(mCanvas, true); |
Mike Reed | 70ffbf9 | 2014-12-08 17:03:30 -0500 | [diff] [blame] | 545 | mCanvas->concat(matrix); |
| 546 | mCanvas->drawBitmap(bitmap, 0, 0, paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 547 | } |
| 548 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 549 | void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, float srcLeft, float srcTop, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 550 | float srcRight, float srcBottom, float dstLeft, float dstTop, |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 551 | float dstRight, float dstBottom, const SkPaint* paint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 552 | SkBitmap bitmap; |
| 553 | hwuiBitmap.getSkBitmap(&bitmap); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 554 | SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom); |
| 555 | SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom); |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 556 | mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 557 | } |
| 558 | |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 559 | void SkiaCanvas::drawBitmapMesh(Bitmap& hwuiBitmap, int meshWidth, int meshHeight, |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 560 | const float* vertices, const int* colors, const SkPaint* paint) { |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 561 | SkBitmap bitmap; |
| 562 | hwuiBitmap.getSkBitmap(&bitmap); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 563 | const int ptCount = (meshWidth + 1) * (meshHeight + 1); |
| 564 | const int indexCount = meshWidth * meshHeight * 6; |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame^] | 565 | uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag; |
| 566 | if (colors) { |
| 567 | flags |= SkVertices::kHasColors_BuilderFlag; |
| 568 | } |
| 569 | SkVertices::Builder builder(SkCanvas::kTriangles_VertexMode, ptCount, indexCount, flags); |
| 570 | memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint)); |
| 571 | if (colors) { |
| 572 | memcpy(builder.colors(), colors, ptCount * sizeof(SkColor)); |
| 573 | } |
| 574 | SkPoint* texs = builder.texCoords(); |
| 575 | uint16_t* indices = builder.indices(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 576 | |
| 577 | // cons up texture coordinates and indices |
| 578 | { |
| 579 | const SkScalar w = SkIntToScalar(bitmap.width()); |
| 580 | const SkScalar h = SkIntToScalar(bitmap.height()); |
| 581 | const SkScalar dx = w / meshWidth; |
| 582 | const SkScalar dy = h / meshHeight; |
| 583 | |
| 584 | SkPoint* texsPtr = texs; |
| 585 | SkScalar y = 0; |
| 586 | for (int i = 0; i <= meshHeight; i++) { |
| 587 | if (i == meshHeight) { |
| 588 | y = h; // to ensure numerically we hit h exactly |
| 589 | } |
| 590 | SkScalar x = 0; |
| 591 | for (int j = 0; j < meshWidth; j++) { |
| 592 | texsPtr->set(x, y); |
| 593 | texsPtr += 1; |
| 594 | x += dx; |
| 595 | } |
| 596 | texsPtr->set(w, y); |
| 597 | texsPtr += 1; |
| 598 | y += dy; |
| 599 | } |
| 600 | SkASSERT(texsPtr - texs == ptCount); |
| 601 | } |
| 602 | |
| 603 | // cons up indices |
| 604 | { |
| 605 | uint16_t* indexPtr = indices; |
| 606 | int index = 0; |
| 607 | for (int i = 0; i < meshHeight; i++) { |
| 608 | for (int j = 0; j < meshWidth; j++) { |
| 609 | // lower-left triangle |
| 610 | *indexPtr++ = index; |
| 611 | *indexPtr++ = index + meshWidth + 1; |
| 612 | *indexPtr++ = index + meshWidth + 2; |
| 613 | // upper-right triangle |
| 614 | *indexPtr++ = index; |
| 615 | *indexPtr++ = index + meshWidth + 2; |
| 616 | *indexPtr++ = index + 1; |
| 617 | // bump to the next cell |
| 618 | index += 1; |
| 619 | } |
| 620 | // bump to the next row |
| 621 | index += 1; |
| 622 | } |
| 623 | SkASSERT(indexPtr - indices == indexCount); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | // double-check that we have legal indices |
| 627 | #ifdef SK_DEBUG |
| 628 | { |
| 629 | for (int i = 0; i < indexCount; i++) { |
| 630 | SkASSERT((unsigned)indices[i] < (unsigned)ptCount); |
| 631 | } |
| 632 | } |
| 633 | #endif |
| 634 | |
| 635 | // cons-up a shader for the bitmap |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 636 | SkPaint tmpPaint; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 637 | if (paint) { |
| 638 | tmpPaint = *paint; |
| 639 | } |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 640 | |
| 641 | sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode); |
| 642 | tmpPaint.setShader(image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode)); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 643 | |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame^] | 644 | mCanvas->drawVertices(builder.detach(), SkBlendMode::kModulate, tmpPaint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 645 | } |
| 646 | |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 647 | void SkiaCanvas::drawNinePatch(Bitmap& hwuiBitmap, const Res_png_9patch& chunk, |
Derek Sollenberger | edca320 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 648 | float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) { |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 649 | |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 650 | SkBitmap bitmap; |
| 651 | hwuiBitmap.getSkBitmap(&bitmap); |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 652 | |
| 653 | SkCanvas::Lattice lattice; |
Matt Sarett | 7de7385 | 2016-10-25 18:36:39 -0400 | [diff] [blame] | 654 | NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height()); |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 655 | |
| 656 | lattice.fFlags = nullptr; |
| 657 | int numFlags = 0; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 658 | if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) { |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 659 | // We can expect the framework to give us a color for every distinct rect. |
| 660 | // Skia requires a flag for every rect. |
| 661 | numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1); |
| 662 | } |
| 663 | |
| 664 | SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags); |
| 665 | if (numFlags > 0) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 666 | NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk); |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | lattice.fBounds = nullptr; |
| 670 | SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom); |
| 671 | mCanvas->drawBitmapLattice(bitmap, lattice, dst, paint); |
Derek Sollenberger | edca320 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 672 | } |
| 673 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 674 | void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) { |
Doris Liu | 1d8e194 | 2016-03-02 15:16:28 -0800 | [diff] [blame] | 675 | vectorDrawable->drawStaging(this); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 678 | // ---------------------------------------------------------------------------- |
| 679 | // Canvas draw operations: Text |
| 680 | // ---------------------------------------------------------------------------- |
| 681 | |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 682 | void SkiaCanvas::drawGlyphs(const uint16_t* text, const float* positions, int count, |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 683 | const SkPaint& paint, float x, float y, |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 684 | float boundsLeft, float boundsTop, float boundsRight, float boundsBottom, |
| 685 | float totalAdvance) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 686 | if (!text || !positions || count <= 0 || paint.nothingToDraw()) return; |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 687 | // Set align to left for drawing, as we don't want individual |
| 688 | // glyphs centered or right-aligned; the offset above takes |
| 689 | // care of all alignment. |
| 690 | SkPaint paintCopy(paint); |
| 691 | paintCopy.setTextAlign(SkPaint::kLeft_Align); |
| 692 | |
| 693 | SkRect bounds = SkRect::MakeLTRB(boundsLeft + x, boundsTop + y, |
| 694 | boundsRight + x, boundsBottom + y); |
| 695 | |
| 696 | SkTextBlobBuilder builder; |
| 697 | const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds); |
| 698 | // TODO: we could reduce the number of memcpy's if the this were exposed further up |
| 699 | // in the architecture. |
| 700 | memcpy(buffer.glyphs, text, count * sizeof(uint16_t)); |
| 701 | memcpy(buffer.pos, positions, (count << 1) * sizeof(float)); |
| 702 | |
| 703 | sk_sp<SkTextBlob> textBlob(builder.make()); |
| 704 | mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy); |
| 705 | drawTextDecorations(x, y, totalAdvance, paintCopy); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 708 | void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, |
| 709 | const SkPaint& paint, const SkPath& path, size_t start, size_t end) { |
| 710 | const int N = end - start; |
Derek Sollenberger | e547dd0 | 2016-11-09 11:55:59 -0500 | [diff] [blame] | 711 | SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform))); |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 712 | SkRSXform* xform = (SkRSXform*)storage.get(); |
| 713 | uint16_t* glyphs = (uint16_t*)(xform + N); |
| 714 | SkPathMeasure meas(path, false); |
| 715 | |
| 716 | for (size_t i = start; i < end; i++) { |
| 717 | glyphs[i - start] = layout.getGlyphId(i); |
| 718 | float x = hOffset + layout.getX(i); |
| 719 | float y = vOffset + layout.getY(i); |
| 720 | |
| 721 | SkPoint pos; |
| 722 | SkVector tan; |
| 723 | if (!meas.getPosTan(x, &pos, &tan)) { |
| 724 | pos.set(x, y); |
| 725 | tan.set(1, 0); |
| 726 | } |
| 727 | xform[i - start].fSCos = tan.x(); |
| 728 | xform[i - start].fSSin = tan.y(); |
| 729 | xform[i - start].fTx = pos.x() - tan.y() * y; |
| 730 | xform[i - start].fTy = pos.y() + tan.x() * y; |
| 731 | } |
| 732 | |
| 733 | this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 734 | } |
| 735 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 736 | // ---------------------------------------------------------------------------- |
| 737 | // Canvas draw operations: Animations |
| 738 | // ---------------------------------------------------------------------------- |
| 739 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 740 | void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, |
| 741 | uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right, |
| 742 | uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx, |
| 743 | uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 744 | sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable( |
| 745 | new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry, paint)); |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 746 | mCanvas->drawDrawable(drawable.get()); |
| 747 | } |
| 748 | |
| 749 | void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y, |
| 750 | uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 751 | sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint)); |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 752 | mCanvas->drawDrawable(drawable.get()); |
| 753 | } |
| 754 | |
| 755 | // ---------------------------------------------------------------------------- |
| 756 | // Canvas draw operations: View System |
| 757 | // ---------------------------------------------------------------------------- |
| 758 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 759 | void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) { |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 760 | LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers"); |
| 761 | } |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 762 | |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 763 | void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) { |
| 764 | LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes"); |
| 765 | } |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 766 | |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 767 | void SkiaCanvas::callDrawGLFunction(Functor* functor, |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 768 | uirenderer::GlFunctorLifecycleListener* listener) { |
| 769 | LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content"); |
| 770 | } |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 771 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 772 | } // namespace android |