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