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