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 18 #include "CanvasProperty.h" 19 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration 20 #include "DeferredLayerUpdater.h" 21 #endif 22 #include <SkCanvas.h> 23 24 #include <cassert> 25 #include <deque> 26 #include <optional> 27 28 #include "RenderNode.h" 29 #include "VectorDrawable.h" 30 #include "hwui/BlurDrawLooper.h" 31 #include "hwui/Canvas.h" 32 #include "hwui/Paint.h" 33 #include "pipeline/skia/AnimatedDrawables.h" 34 35 enum class SkBlendMode; 36 class SkRRect; 37 38 namespace android { 39 40 // Holds an SkCanvas reference plus additional native data. 41 class SkiaCanvas : public Canvas { 42 public: 43 explicit SkiaCanvas(const SkBitmap& bitmap); 44 45 /** 46 * Create a new SkiaCanvas. 47 * 48 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must 49 * not be NULL. This constructor does not take ownership, so the caller 50 * must guarantee that it remains valid while the SkiaCanvas is valid. 51 */ 52 explicit SkiaCanvas(SkCanvas* canvas); 53 54 virtual ~SkiaCanvas(); 55 resetRecording(int width,int height,uirenderer::RenderNode * renderNode)56 virtual void resetRecording(int width, int height, 57 uirenderer::RenderNode* renderNode) override { 58 LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas"); 59 } 60 finishRecording(uirenderer::RenderNode *)61 virtual void finishRecording(uirenderer::RenderNode*) override { 62 LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList"); 63 } enableZ(bool enableZ)64 virtual void enableZ(bool enableZ) override { 65 LOG_ALWAYS_FATAL("SkiaCanvas does not support enableZ"); 66 } 67 68 virtual void punchHole(const SkRRect& rect, float alpha) override; 69 70 virtual void setBitmap(const SkBitmap& bitmap) override; 71 72 virtual bool isOpaque() override; 73 virtual int width() override; 74 virtual int height() override; 75 76 virtual int getSaveCount() const override; 77 virtual int save(SaveFlags::Flags flags) override; 78 virtual void restore() override; 79 virtual void restoreToCount(int saveCount) override; 80 virtual void restoreUnclippedLayer(int saveCount, const Paint& paint) override; 81 82 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) override; 83 virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) override; 84 virtual int saveUnclippedLayer(int left, int top, int right, int bottom) override; 85 86 virtual void getMatrix(SkMatrix* outMatrix) const override; 87 virtual void setMatrix(const SkMatrix& matrix) override; 88 virtual void concat(const SkMatrix& matrix) override; 89 virtual void rotate(float degrees) override; 90 virtual void scale(float sx, float sy) override; 91 virtual void skew(float sx, float sy) override; 92 virtual void translate(float dx, float dy) override; 93 94 virtual bool getClipBounds(SkRect* outRect) const override; 95 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override; 96 virtual bool quickRejectPath(const SkPath& path) const override; 97 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) override; 98 virtual bool clipPath(const SkPath* path, SkClipOp op) override; 99 virtual bool replaceClipRect_deprecated(float left, float top, float right, 100 float bottom) override; 101 virtual bool replaceClipPath_deprecated(const SkPath* path) override; 102 103 virtual PaintFilter* getPaintFilter() override; 104 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) override; 105 106 virtual SkCanvasState* captureCanvasState() const override; 107 108 virtual void drawColor(int color, SkBlendMode mode) override; 109 virtual void drawPaint(const Paint& paint) override; 110 111 virtual void drawPoint(float x, float y, const Paint& paint) override; 112 virtual void drawPoints(const float* points, int count, const Paint& paint) override; 113 virtual void drawLine(float startX, float startY, float stopX, float stopY, 114 const Paint& paint) override; 115 virtual void drawLines(const float* points, int count, const Paint& paint) override; 116 virtual void drawRect(float left, float top, float right, float bottom, 117 const Paint& paint) override; 118 virtual void drawRegion(const SkRegion& region, const Paint& paint) override; 119 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 120 const Paint& paint) override; 121 122 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner, 123 const Paint& paint) override; 124 125 virtual void drawCircle(float x, float y, float radius, const Paint& paint) override; 126 virtual void drawOval(float left, float top, float right, float bottom, 127 const Paint& paint) override; 128 virtual void drawArc(float left, float top, float right, float bottom, float startAngle, 129 float sweepAngle, bool useCenter, const Paint& paint) override; 130 virtual void drawPath(const SkPath& path, const Paint& paint) override; 131 virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) override; 132 virtual void drawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const Paint& paint) override; 133 134 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) override; 135 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) override; 136 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight, 137 float srcBottom, float dstLeft, float dstTop, float dstRight, 138 float dstBottom, const Paint* paint) override; 139 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight, 140 const float* vertices, const int* colors, 141 const Paint* paint) override; 142 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft, 143 float dstTop, float dstRight, float dstBottom, 144 const Paint* paint) override; 145 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) override; 146 147 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override; 148 149 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, 150 uirenderer::CanvasPropertyPrimitive* top, 151 uirenderer::CanvasPropertyPrimitive* right, 152 uirenderer::CanvasPropertyPrimitive* bottom, 153 uirenderer::CanvasPropertyPrimitive* rx, 154 uirenderer::CanvasPropertyPrimitive* ry, 155 uirenderer::CanvasPropertyPaint* paint) override; 156 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, 157 uirenderer::CanvasPropertyPrimitive* y, 158 uirenderer::CanvasPropertyPrimitive* radius, 159 uirenderer::CanvasPropertyPaint* paint) override; 160 virtual void drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) override; 161 162 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override; 163 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override; 164 virtual void drawPicture(const SkPicture& picture) override; 165 166 protected: 167 SkiaCanvas(); asSkCanvas()168 SkCanvas* asSkCanvas() { return mCanvas; } 169 void reset(SkCanvas* skiaCanvas); drawDrawable(SkDrawable * drawable)170 void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); } 171 172 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x, 173 float y, float totalAdvance) override; 174 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, 175 const Paint& paint, const SkPath& path, size_t start, 176 size_t end) override; 177 178 void onFilterPaint(Paint& paint); 179 filterPaint(const Paint & src)180 Paint filterPaint(const Paint& src) { 181 Paint dst(src); 182 this->onFilterPaint(dst); 183 return dst; 184 } 185 186 // proc(const SkPaint& modifiedPaint) 187 template <typename Proc> 188 void applyLooper(const Paint* paint, Proc proc, void (*preFilter)(SkPaint&) = nullptr) { 189 BlurDrawLooper* looper = paint ? paint->getLooper() : nullptr; 190 Paint pnt = paint ? *paint : Paint(); 191 if (preFilter) { 192 preFilter(pnt); 193 } 194 this->onFilterPaint(pnt); 195 if (looper) { 196 looper->apply(pnt, [&](SkPoint offset, const Paint& modifiedPaint) { 197 mCanvas->save(); 198 mCanvas->translate(offset.fX, offset.fY); 199 proc(modifiedPaint); 200 mCanvas->restore(); 201 }); 202 } else { 203 proc(pnt); 204 } 205 } 206 207 private: 208 struct SaveRec { 209 int saveCount; 210 SaveFlags::Flags saveFlags; 211 size_t clipIndex; 212 SaveRecSaveRec213 SaveRec(int saveCount, SaveFlags::Flags saveFlags, size_t clipIndex) 214 : saveCount(saveCount), saveFlags(saveFlags), clipIndex(clipIndex) {} 215 }; 216 217 const SaveRec* currentSaveRec() const; 218 void recordPartialSave(SaveFlags::Flags flags); 219 220 template <typename T> 221 void recordClip(const T&, SkClipOp); 222 void applyPersistentClips(size_t clipStartIndex); 223 224 void drawPoints(const float* points, int count, const Paint& paint, SkCanvas::PointMode mode); 225 226 class Clip; 227 228 std::unique_ptr<SkCanvas> mCanvasOwned; // Might own a canvas we allocated. 229 SkCanvas* mCanvas; // We do NOT own this canvas, it must survive us 230 // unless it is the same as mCanvasOwned.get(). 231 std::unique_ptr<std::deque<SaveRec>> mSaveStack; // Lazily allocated, tracks partial saves. 232 std::vector<Clip> mClipStack; // Tracks persistent clips. 233 sk_sp<PaintFilter> mPaintFilter; 234 }; 235 236 } // namespace android 237