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