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 #include <SaveFlags.h> 22 23 #include <androidfw/ResourceTypes.h> 24 #include "Properties.h" 25 #include "pipeline/skia/AnimatedDrawables.h" 26 #include "utils/Macros.h" 27 28 #include <SkBitmap.h> 29 #include <SkCanvas.h> 30 #include <SkMatrix.h> 31 32 class SkAnimatedImage; 33 class SkCanvasState; 34 class SkRuntimeShaderBuilder; 35 class SkVertices; 36 37 namespace minikin { 38 class Font; 39 class Layout; 40 class MeasuredText; 41 enum class Bidi : uint8_t; 42 } 43 44 namespace android { 45 class PaintFilter; 46 47 namespace uirenderer { 48 class CanvasPropertyPaint; 49 class CanvasPropertyPrimitive; 50 class DeferredLayerUpdater; 51 class RenderNode; 52 namespace VectorDrawable { 53 class Tree; 54 } 55 } 56 typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot; 57 58 typedef std::function<void(uint16_t* text, float* positions)> ReadGlyphFunc; 59 60 class AnimatedImageDrawable; 61 class Bitmap; 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) = 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 rotate(float degrees) = 0; 177 virtual void scale(float sx, float sy) = 0; 178 virtual void skew(float sx, float sy) = 0; 179 virtual void translate(float dx, float dy) = 0; 180 181 // clip 182 virtual bool getClipBounds(SkRect* outRect) const = 0; 183 virtual bool quickRejectRect(float left, float top, float right, float bottom) const = 0; 184 virtual bool quickRejectPath(const SkPath& path) const = 0; 185 186 virtual bool clipRect(float left, float top, float right, float bottom, SkClipOp op) = 0; 187 virtual bool clipPath(const SkPath* path, SkClipOp op) = 0; 188 // Resets clip to wide open, used to emulate the now-removed SkClipOp::kReplace on 189 // apps with compatibility < P. Canvases for version P and later are restricted to 190 // intersect and difference at the Java level, matching SkClipOp's definition. 191 // NOTE: These functions are deprecated and will be removed in a future release 192 virtual bool replaceClipRect_deprecated(float left, float top, float right, float bottom) = 0; 193 virtual bool replaceClipPath_deprecated(const SkPath* path) = 0; 194 195 // filters 196 virtual PaintFilter* getPaintFilter() = 0; 197 virtual void setPaintFilter(sk_sp<PaintFilter> paintFilter) = 0; 198 199 // WebView only captureCanvasState()200 virtual SkCanvasState* captureCanvasState() const { return nullptr; } 201 202 // ---------------------------------------------------------------------------- 203 // Canvas draw operations 204 // ---------------------------------------------------------------------------- 205 virtual void drawColor(int color, SkBlendMode mode) = 0; 206 virtual void drawPaint(const Paint& paint) = 0; 207 208 // Geometry 209 virtual void drawPoint(float x, float y, const Paint& paint) = 0; 210 virtual void drawPoints(const float* points, int floatCount, const Paint& paint) = 0; 211 virtual void drawLine(float startX, float startY, float stopX, float stopY, 212 const Paint& paint) = 0; 213 virtual void drawLines(const float* points, int floatCount, const Paint& paint) = 0; 214 virtual void drawRect(float left, float top, float right, float bottom, 215 const Paint& paint) = 0; 216 virtual void drawRegion(const SkRegion& region, const Paint& paint) = 0; 217 virtual void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, 218 const Paint& paint) = 0; 219 virtual void drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner, 220 const Paint& paint) = 0; 221 virtual void drawCircle(float x, float y, float radius, const Paint& paint) = 0; 222 virtual void drawOval(float left, float top, float right, float bottom, 223 const Paint& paint) = 0; 224 virtual void drawArc(float left, float top, float right, float bottom, float startAngle, 225 float sweepAngle, bool useCenter, const Paint& paint) = 0; 226 virtual void drawPath(const SkPath& path, const Paint& paint) = 0; 227 virtual void drawVertices(const SkVertices*, SkBlendMode, const Paint& paint) = 0; 228 229 // Bitmap-based 230 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) = 0; 231 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) = 0; 232 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight, 233 float srcBottom, float dstLeft, float dstTop, float dstRight, 234 float dstBottom, const Paint* paint) = 0; 235 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight, 236 const float* vertices, const int* colors, const Paint* paint) = 0; 237 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, float dstLeft, 238 float dstTop, float dstRight, float dstBottom, 239 const Paint* paint) = 0; 240 241 virtual double drawAnimatedImage(AnimatedImageDrawable* imgDrawable) = 0; 242 virtual void drawPicture(const SkPicture& picture) = 0; 243 244 /** 245 * Draws a VectorDrawable onto the canvas. 246 */ 247 virtual void drawVectorDrawable(VectorDrawableRoot* tree) = 0; 248 249 void drawGlyphs(const minikin::Font& font, const int* glyphIds, const float* positions, 250 int glyphCount, const Paint& paint); 251 252 /** 253 * Converts utf16 text to glyphs, calculating position and boundary, 254 * and delegating the final draw to virtual drawGlyphs method. 255 */ 256 void drawText(const uint16_t* text, int textSize, int start, int count, int contextStart, 257 int contextCount, float x, float y, minikin::Bidi bidiFlags, 258 const Paint& origPaint, const Typeface* typeface, minikin::MeasuredText* mt); 259 260 void drawTextOnPath(const uint16_t* text, int count, minikin::Bidi bidiFlags, 261 const SkPath& path, float hOffset, float vOffset, const Paint& paint, 262 const Typeface* typeface); 263 264 void drawDoubleRoundRectXY(float outerLeft, float outerTop, float outerRight, 265 float outerBottom, float outerRx, float outerRy, float innerLeft, 266 float innerTop, float innerRight, float innerBottom, float innerRx, 267 float innerRy, const Paint& paint); 268 269 void drawDoubleRoundRectRadii(float outerLeft, float outerTop, float outerRight, 270 float outerBottom, const float* outerRadii, float innerLeft, 271 float innerTop, float innerRight, float innerBottom, 272 const float* innerRadii, const Paint& paint); 273 GetApiLevel()274 static int GetApiLevel() { return sApiLevel; } 275 276 protected: 277 void drawTextDecorations(float x, float y, float length, const Paint& paint); 278 279 /** 280 * glyphFunc: valid only for the duration of the call and should not be cached. 281 * drawText: count is of glyphs 282 * totalAdvance: used to define width of text decorations (underlines, strikethroughs). 283 */ 284 virtual void drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x, 285 float y,float totalAdvance) = 0; 286 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, 287 const Paint& paint, const SkPath& path, size_t start, 288 size_t end) = 0; 289 static int sApiLevel; 290 291 friend class DrawTextFunctor; 292 friend class DrawTextOnPathFunctor; 293 }; 294 295 } // namespace android 296