Canvas.h revision f5d6c555c3430f6e423952ba3ab024380e550bba
1/*
2 * Copyright (C) 2014 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#ifndef ANDROID_GRAPHICS_CANVAS_H
18#define ANDROID_GRAPHICS_CANVAS_H
19
20#include "SkBitmap.h"
21#include "SkCanvas.h"
22#include "SkMatrix.h"
23
24namespace android {
25
26// TODO: move this further up the stack so that all interaction with minikin
27//       happens prior to calling into this interface
28class TypefaceImpl;
29
30class Canvas {
31public:
32    virtual ~Canvas() {};
33
34    static Canvas* create_canvas(SkBitmap* bitmap);
35    static Canvas* create_canvas(SkCanvas* skiaCanvas);
36
37    // TODO: enable HWUI to either create similar canvas wrapper or subclass
38    //       directly from Canvas
39    //static Canvas* create_canvas(uirenderer::Renderer* renderer);
40
41    // TODO: this is a temporary affordance until all necessary logic can be
42    //       moved within this interface! Further, the return value should
43    //       NOT be unref'd and is valid until this canvas is destroyed or a
44    //       new bitmap is set.
45    virtual SkCanvas* getSkCanvas() = 0;
46
47    virtual void setBitmap(SkBitmap* bitmap, bool copyState) = 0;
48
49    virtual bool isOpaque() = 0;
50    virtual int width() = 0;
51    virtual int height() = 0;
52
53// ----------------------------------------------------------------------------
54// Canvas state operations
55// ----------------------------------------------------------------------------
56    // Save (layer)
57    virtual int getSaveCount() const = 0;
58    virtual int save(SkCanvas::SaveFlags flags) = 0;
59    virtual void restore() = 0;
60    virtual void restoreToCount(int saveCount) = 0;
61
62    virtual int saveLayer(float left, float top, float right, float bottom,
63                const SkPaint* paint, SkCanvas::SaveFlags flags) = 0;
64    virtual int saveLayerAlpha(float left, float top, float right, float bottom,
65            int alpha, SkCanvas::SaveFlags flags) = 0;
66
67    // Matrix
68    virtual void getMatrix(SkMatrix* outMatrix) const = 0;
69    virtual void setMatrix(const SkMatrix& matrix) = 0;
70
71    virtual void concat(const SkMatrix& matrix) = 0;
72    virtual void rotate(float degrees) = 0;
73    virtual void scale(float sx, float sy) = 0;
74    virtual void skew(float sx, float sy) = 0;
75    virtual void translate(float dx, float dy) = 0;
76
77    // clip
78    virtual bool getClipBounds(SkRect* outRect) const = 0;
79    virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0;
80    virtual bool quickRejectPath(const SkPath& path) const = 0;
81
82    virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) = 0;
83    virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0;
84    virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0;
85
86    // filters
87    virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0;
88
89// ----------------------------------------------------------------------------
90// Canvas draw operations
91// ----------------------------------------------------------------------------
92    virtual void drawColor(int color, SkXfermode::Mode mode) = 0;
93    virtual void drawPaint(const SkPaint& paint) = 0;
94
95    // Geometry
96    virtual void drawPoint(float x, float y, const SkPaint& paint) = 0;
97    virtual void drawPoints(const float* points, int count, const SkPaint& paint) = 0;
98    virtual void drawLine(float startX, float startY, float stopX, float stopY,
99                const SkPaint& paint) = 0;
100    virtual void drawLines(const float* points, int count, const SkPaint& paint) = 0;
101    virtual void drawRect(float left, float top, float right, float bottom,
102            const SkPaint& paint) = 0;
103    virtual void drawRoundRect(float left, float top, float right, float bottom,
104            float rx, float ry, const SkPaint& paint) = 0;
105    virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0;
106    virtual void drawOval(float left, float top, float right, float bottom,
107            const SkPaint& paint) = 0;
108    virtual void drawArc(float left, float top, float right, float bottom,
109            float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0;
110    virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0;
111    virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
112                              const float* verts, const float* tex, const int* colors,
113                              const uint16_t* indices, int indexCount, const SkPaint& paint) = 0;
114
115    // Bitmap-based
116    virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
117            const SkPaint* paint) = 0;
118    virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
119            const SkPaint* paint) = 0;
120    virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
121            float srcRight, float srcBottom, float dstLeft, float dstTop,
122            float dstRight, float dstBottom, const SkPaint* paint) = 0;
123    virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
124            const float* vertices, const int* colors, const SkPaint* paint) = 0;
125
126    // Text
127    virtual void drawText(const char* text, int start, int count, int contextCount,
128            float x, float y, int bidiFlags, const SkPaint& paint,
129            TypefaceImpl* typeface) = 0;
130    virtual void drawPosText(const char* text, const float* positions, int count, int posCount,
131            const SkPaint& paint) = 0;
132    virtual void drawTextOnPath(const char* text, int count, const SkPath& path,
133            float hOffset, float vOffset, const SkPaint& paint) = 0;
134};
135
136}; // namespace android
137#endif // ANDROID_GRAPHICS_CANVAS_H
138