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