Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 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 | |
John Reck | 849911a | 2015-01-20 07:51:14 -0800 | [diff] [blame] | 20 | #include <cutils/compiler.h> |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 21 | #include <utils/Functor.h> |
John Reck | 849911a | 2015-01-20 07:51:14 -0800 | [diff] [blame] | 22 | |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 23 | #include "utils/NinePatch.h" |
| 24 | |
John Reck | 849911a | 2015-01-20 07:51:14 -0800 | [diff] [blame] | 25 | #include <SkBitmap.h> |
| 26 | #include <SkCanvas.h> |
| 27 | #include <SkMatrix.h> |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 31 | namespace uirenderer { |
| 32 | class CanvasPropertyPaint; |
| 33 | class CanvasPropertyPrimitive; |
| 34 | class DeferredLayerUpdater; |
| 35 | class DisplayList; |
| 36 | class RenderNode; |
| 37 | } |
| 38 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 39 | namespace SaveFlags { |
| 40 | |
| 41 | // These must match the corresponding Canvas API constants. |
| 42 | enum { |
| 43 | Matrix = 0x01, |
| 44 | Clip = 0x02, |
| 45 | HasAlphaLayer = 0x04, |
| 46 | ClipToLayer = 0x10, |
| 47 | |
| 48 | // Helper constant |
| 49 | MatrixClip = Matrix | Clip, |
| 50 | }; |
| 51 | typedef uint32_t Flags; |
| 52 | |
| 53 | } // namespace SaveFlags |
| 54 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 55 | namespace uirenderer { |
| 56 | namespace VectorDrawable { |
| 57 | class Tree; |
| 58 | }; |
| 59 | }; |
| 60 | typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot; |
| 61 | |
John Reck | 849911a | 2015-01-20 07:51:14 -0800 | [diff] [blame] | 62 | class ANDROID_API Canvas { |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 63 | public: |
| 64 | virtual ~Canvas() {}; |
| 65 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 66 | static Canvas* create_canvas(const SkBitmap& bitmap); |
Leon Scroggins III | 1898129 | 2014-12-17 11:30:31 -0500 | [diff] [blame] | 67 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 68 | static Canvas* create_recording_canvas(int width, int height); |
| 69 | |
Leon Scroggins III | 1898129 | 2014-12-17 11:30:31 -0500 | [diff] [blame] | 70 | /** |
| 71 | * Create a new Canvas object which delegates to an SkCanvas. |
| 72 | * |
| 73 | * @param skiaCanvas Must not be NULL. All drawing calls will be |
| 74 | * delegated to this object. This function will call ref() on the |
| 75 | * SkCanvas, and the returned Canvas will unref() it upon |
| 76 | * destruction. |
| 77 | * @return new Canvas object. Will not return NULL. |
| 78 | */ |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 79 | static Canvas* create_canvas(SkCanvas* skiaCanvas); |
| 80 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 81 | /** |
| 82 | * Provides a Skia SkCanvas interface that acts as a proxy to this Canvas. |
| 83 | * It is useful for testing and clients (e.g. Picture/Movie) that expect to |
| 84 | * draw their contents into an SkCanvas. |
| 85 | * |
Tom Hudson | 90fb1f6 | 2015-06-24 09:13:50 -0400 | [diff] [blame] | 86 | * The SkCanvas returned is *only* valid until another Canvas call is made |
| 87 | * that would change state (e.g. matrix or clip). Clients of asSkCanvas() |
| 88 | * are responsible for *not* persisting this pointer. |
| 89 | * |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 90 | * Further, the returned SkCanvas should NOT be unref'd and is valid until |
| 91 | * this canvas is destroyed or a new bitmap is set. |
| 92 | */ |
Derek Sollenberger | b3d50e0 | 2015-01-29 11:19:31 -0500 | [diff] [blame] | 93 | virtual SkCanvas* asSkCanvas() = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 94 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 95 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 96 | virtual void setBitmap(const SkBitmap& bitmap) = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 97 | |
| 98 | virtual bool isOpaque() = 0; |
| 99 | virtual int width() = 0; |
| 100 | virtual int height() = 0; |
| 101 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 102 | // ---------------------------------------------------------------------------- |
| 103 | // View System operations (not exposed in public Canvas API) |
| 104 | // ---------------------------------------------------------------------------- |
| 105 | |
| 106 | virtual void resetRecording(int width, int height) = 0; |
| 107 | virtual uirenderer::DisplayList* finishRecording() = 0; |
| 108 | virtual void insertReorderBarrier(bool enableReorder) = 0; |
| 109 | |
Derek Sollenberger | 6578a98 | 2015-07-13 13:24:29 -0400 | [diff] [blame] | 110 | virtual void setHighContrastText(bool highContrastText) = 0; |
| 111 | virtual bool isHighContrastText() = 0; |
| 112 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 113 | virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, |
| 114 | uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right, |
| 115 | uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx, |
| 116 | uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) = 0; |
| 117 | virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, |
| 118 | uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius, |
| 119 | uirenderer::CanvasPropertyPaint* paint) = 0; |
| 120 | |
| 121 | virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) = 0; |
| 122 | virtual void drawRenderNode(uirenderer::RenderNode* renderNode) = 0; |
| 123 | virtual void callDrawGLFunction(Functor* functor) = 0; |
| 124 | |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 125 | // ---------------------------------------------------------------------------- |
| 126 | // Canvas state operations |
| 127 | // ---------------------------------------------------------------------------- |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 128 | |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 129 | // Save (layer) |
| 130 | virtual int getSaveCount() const = 0; |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 131 | virtual int save(SaveFlags::Flags flags) = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 132 | virtual void restore() = 0; |
| 133 | virtual void restoreToCount(int saveCount) = 0; |
| 134 | |
| 135 | virtual int saveLayer(float left, float top, float right, float bottom, |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 136 | const SkPaint* paint, SaveFlags::Flags flags) = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 137 | virtual int saveLayerAlpha(float left, float top, float right, float bottom, |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 138 | int alpha, SaveFlags::Flags flags) = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 139 | |
| 140 | // Matrix |
| 141 | virtual void getMatrix(SkMatrix* outMatrix) const = 0; |
| 142 | virtual void setMatrix(const SkMatrix& matrix) = 0; |
| 143 | |
| 144 | virtual void concat(const SkMatrix& matrix) = 0; |
| 145 | virtual void rotate(float degrees) = 0; |
| 146 | virtual void scale(float sx, float sy) = 0; |
| 147 | virtual void skew(float sx, float sy) = 0; |
| 148 | virtual void translate(float dx, float dy) = 0; |
| 149 | |
| 150 | // clip |
| 151 | virtual bool getClipBounds(SkRect* outRect) const = 0; |
| 152 | virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0; |
| 153 | virtual bool quickRejectPath(const SkPath& path) const = 0; |
| 154 | |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 155 | virtual bool clipRect(float left, float top, float right, float bottom, |
| 156 | SkRegion::Op op = SkRegion::kIntersect_Op) = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 157 | virtual bool clipPath(const SkPath* path, SkRegion::Op op) = 0; |
| 158 | virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) = 0; |
| 159 | |
| 160 | // filters |
| 161 | virtual SkDrawFilter* getDrawFilter() = 0; |
| 162 | virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0; |
| 163 | |
| 164 | // ---------------------------------------------------------------------------- |
| 165 | // Canvas draw operations |
| 166 | // ---------------------------------------------------------------------------- |
| 167 | virtual void drawColor(int color, SkXfermode::Mode mode) = 0; |
| 168 | virtual void drawPaint(const SkPaint& paint) = 0; |
| 169 | |
| 170 | // Geometry |
| 171 | virtual void drawPoint(float x, float y, const SkPaint& paint) = 0; |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 172 | virtual void drawPoints(const float* points, int floatCount, const SkPaint& paint) = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 173 | virtual void drawLine(float startX, float startY, float stopX, float stopY, |
| 174 | const SkPaint& paint) = 0; |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 175 | virtual void drawLines(const float* points, int floatCount, const SkPaint& paint) = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 176 | virtual void drawRect(float left, float top, float right, float bottom, |
| 177 | const SkPaint& paint) = 0; |
Derek Sollenberger | 94394b3 | 2015-07-10 09:58:41 -0400 | [diff] [blame] | 178 | virtual void drawRegion(const SkRegion& region, const SkPaint& paint) = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 179 | virtual void drawRoundRect(float left, float top, float right, float bottom, |
| 180 | float rx, float ry, const SkPaint& paint) = 0; |
| 181 | virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0; |
| 182 | virtual void drawOval(float left, float top, float right, float bottom, |
| 183 | const SkPaint& paint) = 0; |
| 184 | virtual void drawArc(float left, float top, float right, float bottom, |
| 185 | float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) = 0; |
| 186 | virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0; |
| 187 | virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount, |
| 188 | const float* verts, const float* tex, const int* colors, |
| 189 | const uint16_t* indices, int indexCount, const SkPaint& paint) = 0; |
| 190 | |
| 191 | // Bitmap-based |
| 192 | virtual void drawBitmap(const SkBitmap& bitmap, float left, float top, |
| 193 | const SkPaint* paint) = 0; |
| 194 | virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, |
| 195 | const SkPaint* paint) = 0; |
| 196 | virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop, |
| 197 | float srcRight, float srcBottom, float dstLeft, float dstTop, |
| 198 | float dstRight, float dstBottom, const SkPaint* paint) = 0; |
| 199 | virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight, |
| 200 | const float* vertices, const int* colors, const SkPaint* paint) = 0; |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 201 | virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk, |
| 202 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
| 203 | const SkPaint* paint) = 0; |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 204 | |
| 205 | // Text |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 206 | /** |
| 207 | * drawText: count is of glyphs |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 208 | * totalAdvance: used to define width of text decorations (underlines, strikethroughs). |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 209 | */ |
| 210 | virtual void drawText(const uint16_t* glyphs, const float* positions, int count, |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 211 | const SkPaint& paint, float x, float y, |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 212 | float boundsLeft, float boundsTop, float boundsRight, float boundsBottom, |
| 213 | float totalAdvance) = 0; |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 214 | /** drawTextOnPath: count is of glyphs */ |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 215 | virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path, |
| 216 | float hOffset, float vOffset, const SkPaint& paint) = 0; |
| 217 | |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 218 | /** |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 219 | * Specifies if the positions passed to ::drawText are absolute or relative |
| 220 | * to the (x,y) value provided. |
| 221 | * |
| 222 | * If true the (x,y) values are ignored. Otherwise, those (x,y) values need |
| 223 | * to be added to each glyph's position to get its absolute position. |
| 224 | */ |
| 225 | virtual bool drawTextAbsolutePos() const = 0; |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 226 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 227 | /** |
| 228 | * Draws a VectorDrawable onto the canvas. |
| 229 | */ |
| 230 | virtual void drawVectorDrawable(VectorDrawableRoot* tree); |
| 231 | |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 232 | protected: |
| 233 | void drawTextDecorations(float x, float y, float length, const SkPaint& paint); |
Derek Sollenberger | cae05e0 | 2014-07-24 15:22:13 -0400 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | }; // namespace android |
| 237 | #endif // ANDROID_GRAPHICS_CANVAS_H |