SkiaCanvas.h revision 6e49c9f007c879f05b035c40c0ba543c00f9d0d0
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>
25#include <SkTLazy.h>
26
27namespace android {
28
29// Holds an SkCanvas reference plus additional native data.
30class SkiaCanvas : public Canvas {
31public:
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
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.
40     */
41    explicit SkiaCanvas(SkCanvas* canvas);
42
43    virtual ~SkiaCanvas();
44
45    virtual SkCanvas* asSkCanvas() override {
46        return mCanvas;
47    }
48
49    virtual void resetRecording(int width, int height,
50            uirenderer::RenderNode* renderNode) override {
51        LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
52    }
53
54    virtual uirenderer::DisplayList* finishRecording() override {
55        LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
56        return nullptr;
57    }
58    virtual void insertReorderBarrier(bool enableReorder) override {
59        LOG_ALWAYS_FATAL("SkiaCanvas does not support reordering barriers");
60    }
61
62    virtual void setBitmap(const SkBitmap& bitmap) override;
63
64    virtual bool isOpaque() override;
65    virtual int width() override;
66    virtual int height() override;
67
68    virtual void setHighContrastText(bool highContrastText) override {
69        mHighContrastText = highContrastText;
70    }
71    virtual bool isHighContrastText() override { return mHighContrastText; }
72
73    virtual int getSaveCount() const override;
74    virtual int save(SaveFlags::Flags flags) override;
75    virtual void restore() override;
76    virtual void restoreToCount(int saveCount) override;
77
78    virtual int saveLayer(float left, float top, float right, float bottom,
79                const SkPaint* paint, SaveFlags::Flags flags) override;
80    virtual int saveLayerAlpha(float left, float top, float right, float bottom,
81            int alpha, SaveFlags::Flags flags) override;
82
83    virtual void getMatrix(SkMatrix* outMatrix) const override;
84    virtual void setMatrix(const SkMatrix& matrix) override;
85    virtual void concat(const SkMatrix& matrix) override;
86    virtual void rotate(float degrees) override;
87    virtual void scale(float sx, float sy) override;
88    virtual void skew(float sx, float sy) override;
89    virtual void translate(float dx, float dy) override;
90
91    virtual bool getClipBounds(SkRect* outRect) const override;
92    virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
93    virtual bool quickRejectPath(const SkPath& path) const override;
94    virtual bool clipRect(float left, float top, float right, float bottom,
95            SkClipOp op) override;
96    virtual bool clipPath(const SkPath* path, SkClipOp op) override;
97    virtual bool clipRegion(const SkRegion* region, SkClipOp op) override;
98
99    virtual SkDrawFilter* getDrawFilter() override;
100    virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
101
102    virtual void drawColor(int color, SkBlendMode mode) override;
103    virtual void drawPaint(const SkPaint& paint) override;
104
105    virtual void drawPoint(float x, float y, const SkPaint& paint) override;
106    virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
107    virtual void drawLine(float startX, float startY, float stopX, float stopY,
108            const SkPaint& paint) override;
109    virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
110    virtual void drawRect(float left, float top, float right, float bottom,
111            const SkPaint& paint) override;
112    virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
113    virtual void drawRoundRect(float left, float top, float right, float bottom,
114            float rx, float ry, const SkPaint& paint) override;
115    virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
116    virtual void drawOval(float left, float top, float right, float bottom,
117            const SkPaint& paint) override;
118    virtual void drawArc(float left, float top, float right, float bottom,
119            float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
120    virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
121    virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
122            const float* verts, const float* tex, const int* colors,
123            const uint16_t* indices, int indexCount, const SkPaint& paint) override;
124
125    virtual void drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) override;
126    virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) override;
127    virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop,
128            float srcRight, float srcBottom, float dstLeft, float dstTop,
129            float dstRight, float dstBottom, const SkPaint* paint) override;
130    virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
131            const float* vertices, const int* colors, const SkPaint* paint) override;
132    virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk,
133            float dstLeft, float dstTop, float dstRight, float dstBottom,
134            const SkPaint* paint) override;
135
136    virtual bool drawTextAbsolutePos() const  override { return true; }
137    virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
138
139    virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
140            uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
141            uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
142            uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) override;
143    virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
144            uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
145            uirenderer::CanvasPropertyPaint* paint) override;
146
147    virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
148    virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
149    virtual void callDrawGLFunction(Functor* functor,
150            uirenderer::GlFunctorLifecycleListener* listener) override;
151
152protected:
153    SkiaCanvas();
154    void reset(SkCanvas* skiaCanvas);
155    void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); }
156
157    virtual void drawGlyphs(const uint16_t* text, const float* positions, int count,
158            const SkPaint& paint, float x, float y,
159            float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
160            float totalAdvance) override;
161    virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
162            const SkPaint& paint, const SkPath& path, size_t start, size_t end) override;
163
164private:
165    struct SaveRec {
166        int              saveCount;
167        SaveFlags::Flags saveFlags;
168        size_t           clipIndex;
169    };
170
171    bool mHighContrastText = false;
172
173    const SaveRec* currentSaveRec() const;
174    void recordPartialSave(SaveFlags::Flags flags);
175
176    template<typename T>
177    void recordClip(const T&, SkClipOp);
178    void applyPersistentClips(size_t clipStartIndex);
179
180    void drawPoints(const float* points, int count, const SkPaint& paint,
181            SkCanvas::PointMode mode);
182
183    class Clip;
184
185    std::unique_ptr<SkCanvas> mCanvasOwned; // might own a canvas we allocated
186    SkCanvas*                 mCanvas;    // we do NOT own this canvas, it must survive us
187                                          // unless it is the same as mCanvasOwned.get()
188    std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
189    std::vector<Clip>        mClipStack; // tracks persistent clips.
190};
191
192} // namespace android
193
194