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