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