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 <SaveFlags.h> 20 #include <SkBitmap.h> 21 #include <SkCanvas.h> 22 #include <SkMatrix.h> 23 #include <androidfw/ResourceTypes.h> 24 #include <cutils/compiler.h> 25 #include <utils/Functor.h> 26 27 #include "Properties.h" 28 #include "pipeline/skia/AnimatedDrawables.h" 29 #include "utils/Macros.h" 30 31 enum class SkBlendMode; 32 class SkCanvasState; 33 class SkRRect; 34 class SkVertices; 35 36 namespace minikin { 37 class Font; 38 class Layout; 39 class MeasuredText; 40 enum class Bidi : uint8_t; 41 } 42 43 namespace android { 44 class PaintFilter; 45 46 namespace uirenderer { 47 class CanvasPropertyPaint; 48 class CanvasPropertyPrimitive; 49 class DeferredLayerUpdater; 50 class RenderNode; 51 namespace VectorDrawable { 52 class Tree; 53 } 54 } 55 typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot; 56 57 typedef std::function<void(uint16_t* text, float* positions)> ReadGlyphFunc; 58 59 class AnimatedImageDrawable; 60 class Bitmap; 61 class Mesh; 62 class Paint; 63 struct Typeface; 64 65 enum class DrawTextBlobMode { 66 Normal, 67 HctOutline, 68 HctInner, 69 }; 70 71 inline DrawTextBlobMode gDrawTextBlobMode = DrawTextBlobMode::Normal; 72 73 class ANDROID_API Canvas { 74 public: ~Canvas()75 virtual ~Canvas(){}; 76 77 static Canvas* create_canvas(const SkBitmap& bitmap); 78 79 /** 80 * Create a new Canvas object that records view system drawing operations for deferred 81 * rendering. A canvas returned by this call supports calls to the resetRecording(...) and 82 * finishRecording() calls. The latter call returns a DisplayList that is specific to the 83 * RenderPipeline defined by Properties::getRenderPipelineType(). 84 * 85 * @param width of the requested Canvas. 86 * @param height of the requested Canvas. 87 * @param renderNode is an optional parameter that specifies the node that will consume the 88 * DisplayList produced by the returned Canvas. This enables the reuse of select C++ 89 * objects as a speed optimization. 90 * @return new non-null Canvas Object. The type of DisplayList produced by this canvas is 91 determined based on Properties::getRenderPipelineType(). 92 * 93 */ 94 static WARN_UNUSED_RESULT Canvas* create_recording_canvas( 95 int width, int height, uirenderer::RenderNode* renderNode = nullptr); 96 97 /** 98 * Create a new Canvas object which delegates to an SkCanvas. 99 * 100 * @param skiaCanvas Must not be NULL. All drawing calls will be 101 * delegated to this object. This function will call ref() on the 102 * SkCanvas, and the returned Canvas will unref() it upon 103 * destruction. 104 * @return new non-null Canvas Object. The type of DisplayList produced by this canvas is 105 * determined based on Properties::getRenderPipelineType(). 106 */ 107 static Canvas* create_canvas(SkCanvas* skiaCanvas); 108 109 /** 110 * Sets the target SDK version used to build the app. 111 * 112 * @param apiLevel API level 113 * 114 */ 115 static void setCompatibilityVersion(int apiLevel); 116 117 virtual void setBitmap(const SkBitmap& bitmap) = 0; 118 119 virtual bool isOpaque() = 0; 120 virtual int width() = 0; 121 virtual int height() = 0; 122 123 // ---------------------------------------------------------------------------- 124 // View System operations (not exposed in public Canvas API) 125 // ---------------------------------------------------------------------------- 126 127 virtual void resetRecording(int width, int height, 128 uirenderer::RenderNode* renderNode = nullptr) = 0; 129 virtual void finishRecording(uirenderer::RenderNode* destination) = 0; 130 virtual void enableZ(bool enableZ) = 0; 131 isHighContrastText()132 bool isHighContrastText() const { return uirenderer::Properties::enableHighContrastText; } 133 134 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, 135 uirenderer::CanvasPropertyPrimitive* top, 136 uirenderer::CanvasPropertyPrimitive* right, 137 uirenderer::CanvasPropertyPrimitive* bottom, 138 uirenderer::CanvasPropertyPrimitive* rx, 139 uirenderer::CanvasPropertyPrimitive* ry, 140 uirenderer::CanvasPropertyPaint* paint) = 0; 141 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, 142 uirenderer::CanvasPropertyPrimitive* y, 143 uirenderer::CanvasPropertyPrimitive* radius, 144 uirenderer::CanvasPropertyPaint* paint) = 0; 145 virtual void drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) = 0; 146 147 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) = 0; 148 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) = 0; 149 drawWebViewFunctor(int)150 virtual void drawWebViewFunctor(int /*functor*/) { 151 LOG_ALWAYS_FATAL("Not supported"); 152 } 153 154 virtual void punchHole(const SkRRect& rect, float alpha) = 0; 155 156 // ---------------------------------------------------------------------------- 157 // Canvas state operations 158 // ---------------------------------------------------------------------------- 159 160 // Save (layer) 161 virtual int getSaveCount() const = 0; 162 virtual int save(SaveFlags::Flags flags) = 0; 163 virtual void restore() = 0; 164 virtual void restoreToCount(int saveCount) = 0; 165 virtual void restoreUnclippedLayer(int saveCount, const Paint& paint) = 0; 166 167 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) = 0; 168 virtual int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) = 0; 169 virtual int saveUnclippedLayer(int, int, int, int) = 0; 170 171 // Matrix 172 virtual void getMatrix(SkMatrix* outMatrix) const = 0; 173 virtual void setMatrix(const SkMatrix& matrix) = 0; 174 175 virtual void concat(const SkMatrix& matrix) = 0; 176 virtual void concat(const SkM44& matrix) = 0; 177 virtual void rotate(float degrees) = 0; 178 virtual void scale(float sx, float sy) = 0; 179 virtual void skew(float sx, float sy) = 0; 180 virtual void translate(float dx, float dy) = 0; 181 182 // clip 183 virtual bool getClipBounds(SkRect* outRect) const = 0; 184 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0; 185 virtual bool quickRejectPath(const SkPath& path) const = 0; 186 187 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) = 0; 188 virtual bool clipPath(const SkPath* path, SkClipOp op) = 0; 189 virtual void clipShader(sk_sp<SkShader> shader, SkClipOp op) = 0; 190 // Resets clip to wide open, used to emulate the now-removed SkClipOp::kReplace on 191 // apps with compatibility < P. Canvases for version P and later are restricted to 192 // intersect and difference at the Java level, matching SkClipOp's definition. 193 // NOTE: These functions are deprecated and will be removed in a future release 194 virtual bool replaceClipRect_deprecated(float left, float top, float right, float bottom) = 0; 195 virtual bool replaceClipPath_deprecated(const SkPath* path) = 0; 196 197 // filters 198 virtual PaintFilter* getPaintFilter() = 0; 199 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) = 0; 200 201 // WebView only captureCanvasState()202 virtual SkCanvasState* captureCanvasState() const { return nullptr; } 203 204 // ---------------------------------------------------------------------------- 205 // Canvas draw operations 206 // ---------------------------------------------------------------------------- 207 virtual void drawColor(int color, SkBlendMode mode) = 0; 208 virtual void drawPaint(const Paint& paint) = 0; 209 210 // Geometry 211 virtual void drawPoint(float x, float y, const Paint& paint) = 0; 212 virtual void drawPoints(const float* points, int floatCount, const Paint& paint) = 0; 213 virtual void drawLine(float startX, float startY, float stopX, float stopY, 214 const Paint& paint) = 0; 215 virtual void drawLines(const float* points, int floatCount, const Paint& paint) = 0; 216 virtual void drawRect(float left, float top, float right, float bottom, 217 const Paint& paint) = 0; 218 virtual void drawRegion(const SkRegion& region, const Paint& paint) = 0; 219 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 220 const Paint& paint) = 0; 221 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner, 222 const Paint& paint) = 0; 223 virtual void drawCircle(float x, float y, float radius, const Paint& paint) = 0; 224 virtual void drawOval(float left, float top, float right, float bottom, 225 const Paint& paint) = 0; 226 virtual void drawArc(float left, float top, float right, float bottom, float startAngle, 227 float sweepAngle, bool useCenter, const Paint& paint) = 0; 228 virtual void drawPath(const SkPath& path, const Paint& paint) = 0; 229 virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) = 0; 230 virtual void drawMesh(const Mesh& mesh, sk_sp<SkBlender>, const Paint& paint) = 0; 231 232 // Bitmap-based 233 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) = 0; 234 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) = 0; 235 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight, 236 float srcBottom, float dstLeft, float dstTop, float dstRight, 237 float dstBottom, const Paint* paint) = 0; 238 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight, 239 const float* vertices, const int* colors, const Paint* paint) = 0; 240 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft, 241 float dstTop, float dstRight, float dstBottom, 242 const Paint* paint) = 0; 243 244 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) = 0; 245 virtual void drawPicture(const SkPicture& picture) = 0; 246 247 /** 248 * Draws a VectorDrawable onto the canvas. 249 */ 250 virtual void drawVectorDrawable(VectorDrawableRoot* tree) = 0; 251 252 void drawGlyphs(const minikin::Font& font, const int* glyphIds, const float* positions, 253 int glyphCount, const Paint& paint); 254 255 /** 256 * Converts utf16 text to glyphs, calculating position and boundary, 257 * and delegating the final draw to virtual drawGlyphs method. 258 */ 259 void drawText(const uint16_t* text, int textSize, int start, int count, int contextStart, 260 int contextCount, float x, float y, minikin::Bidi bidiFlags, 261 const Paint& origPaint, const Typeface* typeface, minikin::MeasuredText* mt); 262 263 void drawTextOnPath(const uint16_t* text, int count, minikin::Bidi bidiFlags, 264 const SkPath& path, float hOffset, float vOffset, const Paint& paint, 265 const Typeface* typeface); 266 267 void drawDoubleRoundRectXY(float outerLeft, float outerTop, float outerRight, 268 float outerBottom, float outerRx, float outerRy, float innerLeft, 269 float innerTop, float innerRight, float innerBottom, float innerRx, 270 float innerRy, const Paint& paint); 271 272 void drawDoubleRoundRectRadii(float outerLeft, float outerTop, float outerRight, 273 float outerBottom, const float* outerRadii, float innerLeft, 274 float innerTop, float innerRight, float innerBottom, 275 const float* innerRadii, const Paint& paint); 276 GetApiLevel()277 static int GetApiLevel() { return sApiLevel; } 278 279 protected: 280 void drawTextDecorations(float x, float y, float length, const Paint& paint); 281 282 /** 283 * glyphFunc: valid only for the duration of the call and should not be cached. 284 * drawText: count is of glyphs 285 * totalAdvance: used to define width of text decorations (underlines, strikethroughs). 286 */ 287 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x, 288 float y, float totalAdvance) = 0; 289 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, 290 const Paint& paint, const SkPath& path, size_t start, 291 size_t end) = 0; 292 static int sApiLevel; 293 294 friend class DrawTextFunctor; 295 friend class DrawTextOnPathFunctor; 296 }; 297 298 } // namespace android 299