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 #pragma once 18 19 #include <cutils/compiler.h> 20 #include <utils/Functor.h> 21 22 #include <androidfw/ResourceTypes.h> 23 #include "GlFunctorLifecycleListener.h" 24 #include "Properties.h" 25 #include "utils/Macros.h" 26 27 #include <SkBitmap.h> 28 #include <SkCanvas.h> 29 #include <SkMatrix.h> 30 31 class SkAnimatedImage; 32 class SkCanvasState; 33 class SkVertices; 34 35 namespace minikin { 36 class Layout; 37 class MeasuredText; 38 enum class Bidi : uint8_t; 39 } 40 41 namespace android { 42 43 namespace uirenderer { 44 class CanvasPropertyPaint; 45 class CanvasPropertyPrimitive; 46 class DeferredLayerUpdater; 47 class DisplayList; 48 class RenderNode; 49 } 50 51 namespace SaveFlags { 52 53 // These must match the corresponding Canvas API constants. 54 enum { 55 Matrix = 0x01, 56 Clip = 0x02, 57 HasAlphaLayer = 0x04, 58 ClipToLayer = 0x10, 59 60 // Helper constant 61 MatrixClip = Matrix | Clip, 62 }; 63 typedef uint32_t Flags; 64 65 } // namespace SaveFlags 66 67 namespace uirenderer { 68 class SkiaCanvasProxy; 69 namespace VectorDrawable { 70 class Tree; 71 }; 72 }; 73 typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot; 74 75 typedef std::function<void(uint16_t* text, float* positions)> ReadGlyphFunc; 76 77 class AnimatedImageDrawable; 78 class Bitmap; 79 class Paint; 80 struct Typeface; 81 82 class ANDROID_API Canvas { 83 public: ~Canvas()84 virtual ~Canvas(){}; 85 86 static Canvas* create_canvas(const SkBitmap& bitmap); 87 88 /** 89 * Create a new Canvas object that records view system drawing operations for deferred 90 * rendering. A canvas returned by this call supports calls to the resetRecording(...) and 91 * finishRecording() calls. The latter call returns a DisplayList that is specific to the 92 * RenderPipeline defined by Properties::getRenderPipelineType(). 93 * 94 * @param width of the requested Canvas. 95 * @param height of the requested Canvas. 96 * @param renderNode is an optional parameter that specifies the node that will consume the 97 * DisplayList produced by the returned Canvas. This enables the reuse of select C++ 98 * objects as a speed optimization. 99 * @return new non-null Canvas Object. The type of DisplayList produced by this canvas is 100 determined based on Properties::getRenderPipelineType(). 101 * 102 */ 103 static WARN_UNUSED_RESULT Canvas* create_recording_canvas( 104 int width, int height, uirenderer::RenderNode* renderNode = nullptr); 105 106 /** 107 * Create a new Canvas object which delegates to an SkCanvas. 108 * 109 * @param skiaCanvas Must not be NULL. All drawing calls will be 110 * delegated to this object. This function will call ref() on the 111 * SkCanvas, and the returned Canvas will unref() it upon 112 * destruction. 113 * @return new non-null Canvas Object. The type of DisplayList produced by this canvas is 114 * determined based on Properties::getRenderPipelineType(). 115 */ 116 static Canvas* create_canvas(SkCanvas* skiaCanvas); 117 118 /** 119 * Sets the target SDK version used to build the app. 120 * 121 * @param apiLevel API level 122 * 123 */ 124 static void setCompatibilityVersion(int apiLevel); 125 126 /** 127 * Provides a Skia SkCanvas interface that acts as a proxy to this Canvas. 128 * It is useful for testing and clients (e.g. Picture/Movie) that expect to 129 * draw their contents into an SkCanvas. 130 * 131 * The SkCanvas returned is *only* valid until another Canvas call is made 132 * that would change state (e.g. matrix or clip). Clients of asSkCanvas() 133 * are responsible for *not* persisting this pointer. 134 * 135 * Further, the returned SkCanvas should NOT be unref'd and is valid until 136 * this canvas is destroyed or a new bitmap is set. 137 */ 138 virtual SkCanvas* asSkCanvas() = 0; 139 140 virtual void setBitmap(const SkBitmap& bitmap) = 0; 141 142 virtual bool isOpaque() = 0; 143 virtual int width() = 0; 144 virtual int height() = 0; 145 146 // ---------------------------------------------------------------------------- 147 // View System operations (not exposed in public Canvas API) 148 // ---------------------------------------------------------------------------- 149 150 virtual void resetRecording(int width, int height, 151 uirenderer::RenderNode* renderNode = nullptr) = 0; 152 virtual uirenderer::DisplayList* finishRecording() = 0; 153 virtual void insertReorderBarrier(bool enableReorder) = 0; 154 isHighContrastText()155 bool isHighContrastText() const { return uirenderer::Properties::enableHighContrastText; } 156 157 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, 158 uirenderer::CanvasPropertyPrimitive* top, 159 uirenderer::CanvasPropertyPrimitive* right, 160 uirenderer::CanvasPropertyPrimitive* bottom, 161 uirenderer::CanvasPropertyPrimitive* rx, 162 uirenderer::CanvasPropertyPrimitive* ry, 163 uirenderer::CanvasPropertyPaint* paint) = 0; 164 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, 165 uirenderer::CanvasPropertyPrimitive* y, 166 uirenderer::CanvasPropertyPrimitive* radius, 167 uirenderer::CanvasPropertyPaint* paint) = 0; 168 169 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) = 0; 170 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) = 0; 171 virtual void callDrawGLFunction(Functor* functor, 172 uirenderer::GlFunctorLifecycleListener* listener) = 0; 173 174 // ---------------------------------------------------------------------------- 175 // Canvas state operations 176 // ---------------------------------------------------------------------------- 177 178 // Save (layer) 179 virtual int getSaveCount() const = 0; 180 virtual int save(SaveFlags::Flags flags) = 0; 181 virtual void restore() = 0; 182 virtual void restoreToCount(int saveCount) = 0; 183 184 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint, 185 SaveFlags::Flags flags) = 0; 186 virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, 187 SaveFlags::Flags flags) = 0; 188 189 // Matrix 190 virtual void getMatrix(SkMatrix* outMatrix) const = 0; 191 virtual void setMatrix(const SkMatrix& matrix) = 0; 192 193 virtual void concat(const SkMatrix& matrix) = 0; 194 virtual void rotate(float degrees) = 0; 195 virtual void scale(float sx, float sy) = 0; 196 virtual void skew(float sx, float sy) = 0; 197 virtual void translate(float dx, float dy) = 0; 198 199 // clip 200 virtual bool getClipBounds(SkRect* outRect) const = 0; 201 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0; 202 virtual bool quickRejectPath(const SkPath& path) const = 0; 203 204 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) = 0; 205 virtual bool clipPath(const SkPath* path, SkClipOp op) = 0; 206 207 // filters 208 virtual SkDrawFilter* getDrawFilter() = 0; 209 virtual void setDrawFilter(SkDrawFilter* drawFilter) = 0; 210 211 // WebView only captureCanvasState()212 virtual SkCanvasState* captureCanvasState() const { return nullptr; } 213 214 // ---------------------------------------------------------------------------- 215 // Canvas draw operations 216 // ---------------------------------------------------------------------------- 217 virtual void drawColor(int color, SkBlendMode mode) = 0; 218 virtual void drawPaint(const SkPaint& paint) = 0; 219 220 // Geometry 221 virtual void drawPoint(float x, float y, const SkPaint& paint) = 0; 222 virtual void drawPoints(const float* points, int floatCount, const SkPaint& paint) = 0; 223 virtual void drawLine(float startX, float startY, float stopX, float stopY, 224 const SkPaint& paint) = 0; 225 virtual void drawLines(const float* points, int floatCount, const SkPaint& paint) = 0; 226 virtual void drawRect(float left, float top, float right, float bottom, 227 const SkPaint& paint) = 0; 228 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) = 0; 229 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 230 const SkPaint& paint) = 0; 231 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) = 0; 232 virtual void drawOval(float left, float top, float right, float bottom, 233 const SkPaint& paint) = 0; 234 virtual void drawArc(float left, float top, float right, float bottom, float startAngle, 235 float sweepAngle, bool useCenter, const SkPaint& paint) = 0; 236 virtual void drawPath(const SkPath& path, const SkPaint& paint) = 0; 237 virtual void drawVertices(const SkVertices*, SkBlendMode, const SkPaint& paint) = 0; 238 239 // Bitmap-based 240 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) = 0; 241 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) = 0; 242 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight, 243 float srcBottom, float dstLeft, float dstTop, float dstRight, 244 float dstBottom, const SkPaint* paint) = 0; 245 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight, 246 const float* vertices, const int* colors, const SkPaint* paint) = 0; 247 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft, 248 float dstTop, float dstRight, float dstBottom, 249 const SkPaint* paint) = 0; 250 251 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) = 0; 252 253 /** 254 * Specifies if the positions passed to ::drawText are absolute or relative 255 * to the (x,y) value provided. 256 * 257 * If true the (x,y) values are ignored. Otherwise, those (x,y) values need 258 * to be added to each glyph's position to get its absolute position. 259 */ 260 virtual bool drawTextAbsolutePos() const = 0; 261 262 /** 263 * Draws a VectorDrawable onto the canvas. 264 */ 265 virtual void drawVectorDrawable(VectorDrawableRoot* tree) = 0; 266 267 /** 268 * Converts utf16 text to glyphs, calculating position and boundary, 269 * and delegating the final draw to virtual drawGlyphs method. 270 */ 271 void drawText(const uint16_t* text, int start, int count, int contextCount, float x, float y, 272 minikin::Bidi bidiFlags, const Paint& origPaint, const Typeface* typeface, 273 minikin::MeasuredText* mt); 274 275 void drawTextOnPath(const uint16_t* text, int count, minikin::Bidi bidiFlags, 276 const SkPath& path, float hOffset, float vOffset, const Paint& paint, 277 const Typeface* typeface); 278 GetApiLevel()279 static int GetApiLevel() { return sApiLevel; } 280 281 protected: 282 void drawTextDecorations(float x, float y, float length, const SkPaint& paint); 283 284 /** 285 * glyphFunc: valid only for the duration of the call and should not be cached. 286 * drawText: count is of glyphs 287 * totalAdvance: used to define width of text decorations (underlines, strikethroughs). 288 */ 289 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const SkPaint& paint, float x, 290 float y, float boundsLeft, float boundsTop, float boundsRight, 291 float boundsBottom, float totalAdvance) = 0; 292 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, 293 const SkPaint& paint, const SkPath& path, size_t start, 294 size_t end) = 0; 295 static int sApiLevel; 296 297 friend class DrawTextFunctor; 298 friend class DrawTextOnPathFunctor; 299 friend class uirenderer::SkiaCanvasProxy; 300 }; 301 302 }; // namespace android 303