1 /* 2 * Copyright (C) 2018 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 17 #pragma once 18 19 #include "CanvasTransform.h" 20 #include "hwui/Bitmap.h" 21 #include "utils/Macros.h" 22 #include "utils/TypeLogic.h" 23 24 #include "SkCanvas.h" 25 #include "SkCanvasVirtualEnforcer.h" 26 #include "SkDrawable.h" 27 #include "SkNoDrawCanvas.h" 28 #include "SkPaint.h" 29 #include "SkPath.h" 30 #include "SkRect.h" 31 32 #include "pipeline/skia/AnimatedDrawables.h" 33 34 #include <SkRuntimeEffect.h> 35 #include <vector> 36 37 namespace android { 38 namespace uirenderer { 39 40 namespace skiapipeline { 41 class FunctorDrawable; 42 } 43 44 namespace VectorDrawable { 45 class Tree; 46 } 47 typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot; 48 49 enum class DisplayListOpType : uint8_t { 50 #define X(T) T, 51 #include "DisplayListOps.in" 52 #undef X 53 }; 54 55 struct DisplayListOp { 56 const uint8_t type : 8; 57 const uint32_t skip : 24; 58 }; 59 60 static_assert(sizeof(DisplayListOp) == 4); 61 62 class RecordingCanvas; 63 64 class DisplayListData final { 65 public: DisplayListData()66 DisplayListData() : mHasText(false) {} 67 ~DisplayListData(); 68 69 void draw(SkCanvas* canvas) const; 70 71 void reset(); empty()72 bool empty() const { return fUsed == 0; } 73 74 void applyColorTransform(ColorTransform transform); 75 hasText()76 bool hasText() const { return mHasText; } usedSize()77 size_t usedSize() const { return fUsed; } allocatedSize()78 size_t allocatedSize() const { return fReserved; } 79 80 private: 81 friend class RecordingCanvas; 82 83 void flush(); 84 85 void save(); 86 void saveLayer(const SkRect*, const SkPaint*, const SkImageFilter*, SkCanvas::SaveLayerFlags); 87 void saveBehind(const SkRect*); 88 void restore(); 89 90 void concat(const SkM44&); 91 void setMatrix(const SkM44&); 92 void scale(SkScalar, SkScalar); 93 void translate(SkScalar, SkScalar); 94 void translateZ(SkScalar); 95 96 void clipPath(const SkPath&, SkClipOp, bool aa); 97 void clipRect(const SkRect&, SkClipOp, bool aa); 98 void clipRRect(const SkRRect&, SkClipOp, bool aa); 99 void clipRegion(const SkRegion&, SkClipOp); 100 101 void drawPaint(const SkPaint&); 102 void drawBehind(const SkPaint&); 103 void drawPath(const SkPath&, const SkPaint&); 104 void drawRect(const SkRect&, const SkPaint&); 105 void drawRegion(const SkRegion&, const SkPaint&); 106 void drawOval(const SkRect&, const SkPaint&); 107 void drawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&); 108 void drawRRect(const SkRRect&, const SkPaint&); 109 void drawDRRect(const SkRRect&, const SkRRect&, const SkPaint&); 110 111 void drawAnnotation(const SkRect&, const char*, SkData*); 112 void drawDrawable(SkDrawable*, const SkMatrix*); 113 void drawPicture(const SkPicture*, const SkMatrix*, const SkPaint*); 114 115 void drawTextBlob(const SkTextBlob*, SkScalar, SkScalar, const SkPaint&); 116 117 void drawImage(sk_sp<const SkImage>, SkScalar, SkScalar, const SkSamplingOptions&, 118 const SkPaint*, BitmapPalette palette); 119 void drawImageNine(sk_sp<const SkImage>, const SkIRect&, const SkRect&, const SkPaint*); 120 void drawImageRect(sk_sp<const SkImage>, const SkRect*, const SkRect&, const SkSamplingOptions&, 121 const SkPaint*, SkCanvas::SrcRectConstraint, BitmapPalette palette); 122 void drawImageLattice(sk_sp<const SkImage>, const SkCanvas::Lattice&, const SkRect&, 123 SkFilterMode, const SkPaint*, BitmapPalette); 124 125 void drawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode, 126 const SkPaint&); 127 void drawPoints(SkCanvas::PointMode, size_t, const SkPoint[], const SkPaint&); 128 void drawVertices(const SkVertices*, SkBlendMode, const SkPaint&); 129 void drawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int, 130 SkBlendMode, const SkSamplingOptions&, const SkRect*, const SkPaint*); 131 void drawRippleDrawable(const skiapipeline::RippleDrawableParams& params); 132 void drawShadowRec(const SkPath&, const SkDrawShadowRec&); 133 void drawVectorDrawable(VectorDrawableRoot* tree); 134 void drawWebView(skiapipeline::FunctorDrawable*); 135 136 template <typename T, typename... Args> 137 void* push(size_t, Args&&...); 138 139 template <typename Fn, typename... Args> 140 void map(const Fn[], Args...) const; 141 142 SkAutoTMalloc<uint8_t> fBytes; 143 size_t fUsed = 0; 144 size_t fReserved = 0; 145 146 bool mHasText : 1; 147 }; 148 149 class RecordingCanvas final : public SkCanvasVirtualEnforcer<SkNoDrawCanvas> { 150 public: 151 RecordingCanvas(); 152 void reset(DisplayListData*, const SkIRect& bounds); 153 154 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override; 155 156 void willSave() override; 157 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override; 158 void willRestore() override; 159 bool onDoSaveBehind(const SkRect*) override; 160 161 void onFlush() override; 162 163 void didConcat44(const SkM44&) override; 164 void didSetM44(const SkM44&) override; 165 void didScale(SkScalar, SkScalar) override; 166 void didTranslate(SkScalar, SkScalar) override; 167 168 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override; 169 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override; 170 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override; 171 void onClipRegion(const SkRegion&, SkClipOp) override; 172 173 void onDrawPaint(const SkPaint&) override; 174 void onDrawBehind(const SkPaint&) override; 175 void onDrawPath(const SkPath&, const SkPaint&) override; 176 void onDrawRect(const SkRect&, const SkPaint&) override; 177 void onDrawRegion(const SkRegion&, const SkPaint&) override; 178 void onDrawOval(const SkRect&, const SkPaint&) override; 179 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override; 180 void onDrawRRect(const SkRRect&, const SkPaint&) override; 181 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; 182 183 void onDrawDrawable(SkDrawable*, const SkMatrix*) override; 184 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override; 185 void onDrawAnnotation(const SkRect&, const char[], SkData*) override; 186 187 void onDrawTextBlob(const SkTextBlob*, SkScalar, SkScalar, const SkPaint&) override; 188 189 void drawImage(const sk_sp<SkImage>&, SkScalar left, SkScalar top, const SkSamplingOptions&, 190 const SkPaint* paint, BitmapPalette pallete); 191 void drawRippleDrawable(const skiapipeline::RippleDrawableParams& params); 192 193 void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst, 194 const SkSamplingOptions&, const SkPaint*, SrcRectConstraint, BitmapPalette); 195 void drawImageLattice(const sk_sp<SkImage>& image, const Lattice& lattice, const SkRect& dst, 196 SkFilterMode, const SkPaint* paint, BitmapPalette palette); 197 198 void onDrawImage2(const SkImage*, SkScalar, SkScalar, const SkSamplingOptions&, 199 const SkPaint*) override; 200 void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect&, SkFilterMode, 201 const SkPaint*) override; 202 void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&, 203 const SkPaint*, SrcRectConstraint) override; 204 205 void onDrawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode, 206 const SkPaint&) override; 207 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override; 208 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override; 209 void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int, 210 SkBlendMode, const SkSamplingOptions&, const SkRect*, const SkPaint*) override; 211 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override; 212 213 void drawVectorDrawable(VectorDrawableRoot* tree); 214 void drawWebView(skiapipeline::FunctorDrawable*); 215 216 /** 217 * If "isClipMayBeComplex" returns false, it is guaranteed the current clip is a rectangle. 218 * If the return value is true, then clip may or may not be complex (there is no guarantee). 219 */ isClipMayBeComplex()220 inline bool isClipMayBeComplex() { return mClipMayBeComplex; } 221 222 private: 223 typedef SkCanvasVirtualEnforcer<SkNoDrawCanvas> INHERITED; 224 setClipMayBeComplex()225 inline void setClipMayBeComplex() { 226 if (!mClipMayBeComplex) { 227 mComplexSaveCount = mSaveCount; 228 mClipMayBeComplex = true; 229 } 230 } 231 232 DisplayListData* fDL; 233 234 /** 235 * mClipMayBeComplex tracks if the current clip is a rectangle. This flag is used to promote 236 * FunctorDrawable to a layer, if it is clipped by a non-rect. 237 */ 238 bool mClipMayBeComplex = false; 239 240 /** 241 * mSaveCount is the current level of our save tree. 242 */ 243 int mSaveCount = 0; 244 245 /** 246 * mComplexSaveCount is the first save level, which has a complex clip. Every level below 247 * mComplexSaveCount is assumed to have a complex clip and every level above mComplexSaveCount 248 * is guaranteed to not be complex. 249 */ 250 int mComplexSaveCount = 0; 251 }; 252 253 } // namespace uirenderer 254 } // namespace android 255