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" |
| 19 | #include "DeferredLayerUpdater.h" |
| 20 | #include "RenderNode.h" |
| 21 | #include "VectorDrawable.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include "hwui/Canvas.h" |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 23 | |
| 24 | #include <SkCanvas.h> |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 25 | #include <SkTLazy.h> |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | // Holds an SkCanvas reference plus additional native data. |
| 30 | class SkiaCanvas : public Canvas { |
| 31 | public: |
| 32 | explicit SkiaCanvas(const SkBitmap& bitmap); |
| 33 | |
| 34 | /** |
| 35 | * Create a new SkiaCanvas. |
| 36 | * |
| 37 | * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 38 | * not be NULL. This constructor does not take ownership, so the caller |
| 39 | * must guarantee that it remains valid while the SkiaCanvas is valid. |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 40 | */ |
Derek Sollenberger | fa3e340 | 2017-08-04 08:35:10 -0400 | [diff] [blame] | 41 | explicit SkiaCanvas(SkCanvas* canvas); |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 42 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 43 | virtual ~SkiaCanvas(); |
| 44 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 45 | virtual SkCanvas* asSkCanvas() override { return mCanvas; } |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 46 | |
Stan Iliev | c0e7a90 | 2016-10-13 17:07:09 -0400 | [diff] [blame] | 47 | virtual void resetRecording(int width, int height, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 48 | uirenderer::RenderNode* renderNode) override { |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 49 | LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas"); |
| 50 | } |
| 51 | |
| 52 | virtual uirenderer::DisplayList* finishRecording() override { |
| 53 | LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList"); |
| 54 | return nullptr; |
| 55 | } |
| 56 | virtual void insertReorderBarrier(bool enableReorder) override { |
| 57 | LOG_ALWAYS_FATAL("SkiaCanvas does not support reordering barriers"); |
| 58 | } |
| 59 | |
| 60 | virtual void setBitmap(const SkBitmap& bitmap) override; |
| 61 | |
| 62 | virtual bool isOpaque() override; |
| 63 | virtual int width() override; |
| 64 | virtual int height() override; |
| 65 | |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 66 | virtual int getSaveCount() const override; |
| 67 | virtual int save(SaveFlags::Flags flags) override; |
| 68 | virtual void restore() override; |
| 69 | virtual void restoreToCount(int saveCount) override; |
| 70 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 71 | virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint, |
| 72 | SaveFlags::Flags flags) override; |
| 73 | virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, |
| 74 | SaveFlags::Flags flags) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 75 | |
| 76 | virtual void getMatrix(SkMatrix* outMatrix) const override; |
| 77 | virtual void setMatrix(const SkMatrix& matrix) override; |
| 78 | virtual void concat(const SkMatrix& matrix) override; |
| 79 | virtual void rotate(float degrees) override; |
| 80 | virtual void scale(float sx, float sy) override; |
| 81 | virtual void skew(float sx, float sy) override; |
| 82 | virtual void translate(float dx, float dy) override; |
| 83 | |
| 84 | virtual bool getClipBounds(SkRect* outRect) const override; |
| 85 | virtual bool quickRejectRect(float left, float top, float right, float bottom) const override; |
| 86 | virtual bool quickRejectPath(const SkPath& path) const override; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 87 | 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] | 88 | virtual bool clipPath(const SkPath* path, SkClipOp op) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 89 | |
| 90 | virtual SkDrawFilter* getDrawFilter() override; |
| 91 | virtual void setDrawFilter(SkDrawFilter* drawFilter) override; |
| 92 | |
Matt Sarett | d0814db | 2017-04-13 09:33:18 -0400 | [diff] [blame] | 93 | virtual SkCanvasState* captureCanvasState() const override; |
| 94 | |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 95 | virtual void drawColor(int color, SkBlendMode mode) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 96 | virtual void drawPaint(const SkPaint& paint) override; |
| 97 | |
| 98 | virtual void drawPoint(float x, float y, const SkPaint& paint) override; |
| 99 | virtual void drawPoints(const float* points, int count, const SkPaint& paint) override; |
| 100 | virtual void drawLine(float startX, float startY, float stopX, float stopY, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 101 | const SkPaint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 102 | virtual void drawLines(const float* points, int count, const SkPaint& paint) override; |
| 103 | virtual void drawRect(float left, float top, float right, float bottom, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 104 | const SkPaint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 105 | virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 106 | virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, |
| 107 | const SkPaint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 108 | virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override; |
| 109 | virtual void drawOval(float left, float top, float right, float bottom, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 110 | const SkPaint& paint) override; |
| 111 | virtual void drawArc(float left, float top, float right, float bottom, float startAngle, |
| 112 | float sweepAngle, bool useCenter, const SkPaint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 113 | virtual void drawPath(const SkPath& path, const SkPaint& paint) override; |
Mike Reed | 826deef | 2017-04-04 15:32:04 -0400 | [diff] [blame] | 114 | virtual void drawVertices(const SkVertices*, SkBlendMode, const SkPaint& paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 115 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 116 | virtual void drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) override; |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 117 | virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) override; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 118 | virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight, |
| 119 | float srcBottom, float dstLeft, float dstTop, float dstRight, |
| 120 | float dstBottom, const SkPaint* paint) override; |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 121 | virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 122 | const float* vertices, const int* colors, |
| 123 | const SkPaint* paint) override; |
| 124 | virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft, |
| 125 | float dstTop, float dstRight, float dstBottom, |
| 126 | const SkPaint* paint) override; |
Derek Sollenberger | 2d14213 | 2018-01-22 10:25:26 -0500 | [diff] [blame] | 127 | virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 128 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 129 | virtual bool drawTextAbsolutePos() const override { return true; } |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 130 | virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override; |
| 131 | |
| 132 | virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 133 | uirenderer::CanvasPropertyPrimitive* top, |
| 134 | uirenderer::CanvasPropertyPrimitive* right, |
| 135 | uirenderer::CanvasPropertyPrimitive* bottom, |
| 136 | uirenderer::CanvasPropertyPrimitive* rx, |
| 137 | uirenderer::CanvasPropertyPrimitive* ry, |
| 138 | uirenderer::CanvasPropertyPaint* paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 139 | virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 140 | uirenderer::CanvasPropertyPrimitive* y, |
| 141 | uirenderer::CanvasPropertyPrimitive* radius, |
| 142 | uirenderer::CanvasPropertyPaint* paint) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 143 | |
| 144 | virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override; |
| 145 | virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override; |
| 146 | virtual void callDrawGLFunction(Functor* functor, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 147 | uirenderer::GlFunctorLifecycleListener* listener) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 148 | |
| 149 | protected: |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 150 | SkiaCanvas(); |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 151 | void reset(SkCanvas* skiaCanvas); |
| 152 | void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); } |
| 153 | |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 154 | virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const SkPaint& paint, float x, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 155 | float y, float boundsLeft, float boundsTop, float boundsRight, |
| 156 | float boundsBottom, float totalAdvance) override; |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 157 | virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 158 | const SkPaint& paint, const SkPath& path, size_t start, |
| 159 | size_t end) override; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | struct SaveRec { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 163 | int saveCount; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 164 | SaveFlags::Flags saveFlags; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 165 | size_t clipIndex; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 166 | }; |
| 167 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 168 | const SaveRec* currentSaveRec() const; |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 169 | void recordPartialSave(SaveFlags::Flags flags); |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 170 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 171 | template <typename T> |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 172 | void recordClip(const T&, SkClipOp); |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 173 | void applyPersistentClips(size_t clipStartIndex); |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 174 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 175 | void drawPoints(const float* points, int count, const SkPaint& paint, SkCanvas::PointMode mode); |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 176 | |
Derek Sollenberger | fa3e340 | 2017-08-04 08:35:10 -0400 | [diff] [blame] | 177 | const SkPaint* addFilter(const SkPaint* origPaint, SkPaint* tmpPaint, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 178 | sk_sp<SkColorFilter> colorSpaceFilter); |
Derek Sollenberger | fa3e340 | 2017-08-04 08:35:10 -0400 | [diff] [blame] | 179 | |
Stan Iliev | f50806a | 2016-10-24 10:40:39 -0400 | [diff] [blame] | 180 | class Clip; |
| 181 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 182 | std::unique_ptr<SkCanvas> mCanvasWrapper; // might own a wrapper on the canvas |
| 183 | std::unique_ptr<SkCanvas> mCanvasOwned; // might own a canvas we allocated |
| 184 | SkCanvas* mCanvas; // we do NOT own this canvas, it must survive us |
| 185 | // unless it is the same as mCanvasOwned.get() |
| 186 | std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves. |
| 187 | std::vector<Clip> mClipStack; // tracks persistent clips. |
Derek Sollenberger | c190813 | 2016-07-15 10:28:16 -0400 | [diff] [blame] | 188 | }; |
| 189 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 190 | } // namespace android |