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