Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #pragma once |
| 17 | |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 18 | #include "CanvasProperty.h" |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 19 | #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 20 | #include "DeferredLayerUpdater.h" |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 21 | #endif |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 22 | #include "RenderNode.h" |
| 23 | #include "VectorDrawable.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 24 | #include "hwui/Canvas.h" |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 25 | #include "hwui/Paint.h" |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 26 | |
| 27 | #include <SkCanvas.h> |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 28 | #include "src/core/SkArenaAlloc.h" |
Ben Wagner | 0ed10be | 2018-06-28 17:08:16 -0400 | [diff] [blame] | 29 | |
| 30 | #include <cassert> |
| 31 | #include <optional> |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | // Holds an SkCanvas reference plus additional native data. |
| 36 | class SkiaCanvas : public Canvas { |
| 37 | public: |
| 38 | explicit SkiaCanvas(const SkBitmap& bitmap); |
| 39 | |
| 40 | /** |
| 41 | * Create a new SkiaCanvas. |
| 42 | * |
| 43 | * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 44 | * not be NULL. This constructor does not take ownership, so the caller |
| 45 | * must guarantee that it remains valid while the SkiaCanvas is valid. |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 46 | */ |
Derek Sollenberger | fa3e340 | 2017-08-04 08:35:10 -0400 | [diff] [blame] | 47 | explicit SkiaCanvas(SkCanvas* canvas); |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 48 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 49 | virtual ~SkiaCanvas(); |
| 50 | |
Stan Iliev | c0e7a90 | 2016-10-13 17:07:09 -0400 | [diff] [blame] | 51 | virtual void resetRecording(int width, int height, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 52 | uirenderer::RenderNode* renderNode) override { |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 53 | LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas"); |
| 54 | } |
| 55 | |
| 56 | virtual uirenderer::DisplayList* finishRecording() override { |
| 57 | LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList"); |
| 58 | return nullptr; |
| 59 | } |
| 60 | virtual void insertReorderBarrier(bool enableReorder) override { |
| 61 | LOG_ALWAYS_FATAL("SkiaCanvas does not support reordering barriers"); |
| 62 | } |
| 63 | |
| 64 | virtual void setBitmap(const SkBitmap& bitmap) override; |
| 65 | |
| 66 | virtual bool isOpaque() override; |
| 67 | virtual int width() override; |
| 68 | virtual int height() override; |
| 69 | |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 70 | virtual int getSaveCount() const override; |
| 71 | virtual int save(SaveFlags::Flags flags) override; |
| 72 | virtual void restore() override; |
| 73 | virtual void restoreToCount(int saveCount) override; |
Derek Sollenberger | ac33a48 | 2019-04-22 16:28:09 -0400 | [diff] [blame] | 74 | virtual void restoreUnclippedLayer(int saveCount, const SkPaint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 75 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint, |
| 77 | SaveFlags::Flags flags) override; |
| 78 | virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, |
| 79 | SaveFlags::Flags flags) override; |
Derek Sollenberger | 24fc901 | 2018-12-07 14:12:12 -0500 | [diff] [blame] | 80 | virtual int saveUnclippedLayer(int left, int top, int right, int bottom) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 81 | |
| 82 | virtual void getMatrix(SkMatrix* outMatrix) const override; |
| 83 | virtual void setMatrix(const SkMatrix& matrix) override; |
| 84 | virtual void concat(const SkMatrix& matrix) override; |
| 85 | virtual void rotate(float degrees) override; |
| 86 | virtual void scale(float sx, float sy) override; |
| 87 | virtual void skew(float sx, float sy) override; |
| 88 | virtual void translate(float dx, float dy) override; |
| 89 | |
| 90 | virtual bool getClipBounds(SkRect* outRect) const override; |
| 91 | virtual bool quickRejectRect(float left, float top, float right, float bottom) const override; |
| 92 | virtual bool quickRejectPath(const SkPath& path) const override; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 93 | virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) override; |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 94 | virtual bool clipPath(const SkPath* path, SkClipOp op) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 95 | |
Ben Wagner | 0ed10be | 2018-06-28 17:08:16 -0400 | [diff] [blame] | 96 | virtual PaintFilter* getPaintFilter() override; |
| 97 | virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 98 | |
Matt Sarett | d0814db | 2017-04-13 09:33:18 -0400 | [diff] [blame] | 99 | virtual SkCanvasState* captureCanvasState() const override; |
| 100 | |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 101 | virtual void drawColor(int color, SkBlendMode mode) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 102 | virtual void drawPaint(const SkPaint& paint) override; |
| 103 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 104 | virtual void drawPoint(float x, float y, const Paint& paint) override; |
| 105 | virtual void drawPoints(const float* points, int count, const Paint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 106 | virtual void drawLine(float startX, float startY, float stopX, float stopY, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 107 | const Paint& paint) override; |
| 108 | virtual void drawLines(const float* points, int count, const Paint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 109 | virtual void drawRect(float left, float top, float right, float bottom, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 110 | const Paint& paint) override; |
| 111 | virtual void drawRegion(const SkRegion& region, const Paint& paint) override; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 112 | virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 113 | const Paint& paint) override; |
Nader Jawad | adfe1d9 | 2018-09-27 12:27:36 -0700 | [diff] [blame] | 114 | |
| 115 | virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 116 | const Paint& paint) override; |
Nader Jawad | adfe1d9 | 2018-09-27 12:27:36 -0700 | [diff] [blame] | 117 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 118 | virtual void drawCircle(float x, float y, float radius, const Paint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 119 | virtual void drawOval(float left, float top, float right, float bottom, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 120 | const Paint& paint) override; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 121 | virtual void drawArc(float left, float top, float right, float bottom, float startAngle, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 122 | float sweepAngle, bool useCenter, const Paint& paint) override; |
| 123 | virtual void drawPath(const SkPath& path, const Paint& paint) override; |
| 124 | virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 125 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 126 | virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) override; |
| 127 | virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) override; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 128 | virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight, |
| 129 | float srcBottom, float dstLeft, float dstTop, float dstRight, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 130 | float dstBottom, const Paint* paint) override; |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 131 | virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 132 | const float* vertices, const int* colors, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 133 | const Paint* paint) override; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 134 | virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft, |
| 135 | float dstTop, float dstRight, float dstBottom, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 136 | const Paint* paint) override; |
Derek Sollenberger | 2d14213 | 2018-01-22 10:25:26 -0500 | [diff] [blame] | 137 | virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 138 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 139 | virtual bool drawTextAbsolutePos() const override { return true; } |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 140 | virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override; |
| 141 | |
| 142 | virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 143 | uirenderer::CanvasPropertyPrimitive* top, |
| 144 | uirenderer::CanvasPropertyPrimitive* right, |
| 145 | uirenderer::CanvasPropertyPrimitive* bottom, |
| 146 | uirenderer::CanvasPropertyPrimitive* rx, |
| 147 | uirenderer::CanvasPropertyPrimitive* ry, |
| 148 | uirenderer::CanvasPropertyPaint* paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 149 | virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 150 | uirenderer::CanvasPropertyPrimitive* y, |
| 151 | uirenderer::CanvasPropertyPrimitive* radius, |
| 152 | uirenderer::CanvasPropertyPaint* paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 153 | |
| 154 | virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override; |
| 155 | virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override; |
| 156 | virtual void callDrawGLFunction(Functor* functor, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 157 | uirenderer::GlFunctorLifecycleListener* listener) override; |
John Reck | 894e85a | 2019-07-15 16:16:44 -0700 | [diff] [blame] | 158 | virtual void drawPicture(const SkPicture& picture) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 159 | |
| 160 | protected: |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 161 | SkiaCanvas(); |
John Reck | 894e85a | 2019-07-15 16:16:44 -0700 | [diff] [blame] | 162 | SkCanvas* asSkCanvas() { return mCanvas; } |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 163 | void reset(SkCanvas* skiaCanvas); |
| 164 | void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); } |
| 165 | |
Mike Reed | 2dfd55d | 2019-01-08 16:19:03 -0500 | [diff] [blame] | 166 | virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 167 | float y, float boundsLeft, float boundsTop, float boundsRight, |
| 168 | float boundsBottom, float totalAdvance) override; |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 169 | virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, |
Mike Reed | 2dfd55d | 2019-01-08 16:19:03 -0500 | [diff] [blame] | 170 | const Paint& paint, const SkPath& path, size_t start, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 171 | size_t end) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 172 | |
Ben Wagner | 0ed10be | 2018-06-28 17:08:16 -0400 | [diff] [blame] | 173 | /** This class acts as a copy on write SkPaint. |
| 174 | * |
| 175 | * Initially this will be the SkPaint passed to the contructor. |
| 176 | * The first time writable() is called this will become a copy of the |
| 177 | * initial SkPaint (or a default SkPaint if nullptr). |
| 178 | */ |
| 179 | struct PaintCoW { |
| 180 | PaintCoW(const SkPaint& that) : mPtr(&that) {} |
| 181 | PaintCoW(const SkPaint* ptr) : mPtr(ptr) {} |
| 182 | PaintCoW(const PaintCoW&) = delete; |
| 183 | PaintCoW(PaintCoW&&) = delete; |
| 184 | PaintCoW& operator=(const PaintCoW&) = delete; |
| 185 | PaintCoW& operator=(PaintCoW&&) = delete; |
| 186 | SkPaint& writeable() { |
| 187 | if (!mStorage) { |
| 188 | if (!mPtr) { |
| 189 | mStorage.emplace(); |
| 190 | } else { |
| 191 | mStorage.emplace(*mPtr); |
| 192 | } |
| 193 | mPtr = &*mStorage; |
| 194 | } |
| 195 | return *mStorage; |
| 196 | } |
| 197 | operator const SkPaint*() const { return mPtr; } |
| 198 | const SkPaint* operator->() const { assert(mPtr); return mPtr; } |
| 199 | const SkPaint& operator*() const { assert(mPtr); return *mPtr; } |
| 200 | explicit operator bool() { return mPtr != nullptr; } |
| 201 | private: |
| 202 | const SkPaint* mPtr; |
| 203 | std::optional<SkPaint> mStorage; |
| 204 | }; |
| 205 | |
| 206 | /** Filters the paint using the current paint filter. |
| 207 | * |
| 208 | * @param paint the paint to filter. Will be initialized with the default |
| 209 | * SkPaint before filtering if filtering is required. |
| 210 | */ |
| 211 | PaintCoW&& filterPaint(PaintCoW&& paint) const; |
| 212 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 213 | template <typename Proc> void apply_looper(const Paint* paint, Proc proc) { |
| 214 | SkPaint skp; |
| 215 | SkDrawLooper* looper = nullptr; |
| 216 | if (paint) { |
| 217 | skp = *filterPaint(paint); |
| 218 | looper = paint->getLooper(); |
| 219 | } |
| 220 | if (looper) { |
| 221 | SkSTArenaAlloc<256> alloc; |
| 222 | SkDrawLooper::Context* ctx = looper->makeContext(&alloc); |
| 223 | if (ctx) { |
| 224 | SkDrawLooper::Context::Info info; |
| 225 | for (;;) { |
| 226 | SkPaint p = skp; |
| 227 | if (!ctx->next(&info, &p)) { |
| 228 | break; |
| 229 | } |
| 230 | mCanvas->save(); |
| 231 | mCanvas->translate(info.fTranslate.fX, info.fTranslate.fY); |
| 232 | proc(p); |
| 233 | mCanvas->restore(); |
| 234 | } |
| 235 | } |
| 236 | } else { |
| 237 | proc(skp); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 242 | private: |
| 243 | struct SaveRec { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 244 | int saveCount; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 245 | SaveFlags::Flags saveFlags; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 246 | size_t clipIndex; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 247 | }; |
| 248 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 249 | const SaveRec* currentSaveRec() const; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 250 | void recordPartialSave(SaveFlags::Flags flags); |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 251 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 252 | template <typename T> |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 253 | void recordClip(const T&, SkClipOp); |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 254 | void applyPersistentClips(size_t clipStartIndex); |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 255 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 256 | void drawPoints(const float* points, int count, const Paint& paint, SkCanvas::PointMode mode); |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 257 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 258 | class Clip; |
| 259 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 260 | std::unique_ptr<SkCanvas> mCanvasOwned; // might own a canvas we allocated |
| 261 | SkCanvas* mCanvas; // we do NOT own this canvas, it must survive us |
| 262 | // unless it is the same as mCanvasOwned.get() |
| 263 | std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves. |
| 264 | std::vector<Clip> mClipStack; // tracks persistent clips. |
Ben Wagner | 0ed10be | 2018-06-28 17:08:16 -0400 | [diff] [blame] | 265 | sk_sp<PaintFilter> mPaintFilter; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 266 | }; |
| 267 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 268 | } // namespace android |