SkiaCanvas.h revision f50806afb8982acbab4510a8d04bbcf13a2bcd9e
1/* 2 * Copyright (C) 2016 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#pragma once 17 18#include "hwui/Canvas.h" 19#include "CanvasProperty.h" 20#include "DeferredLayerUpdater.h" 21#include "RenderNode.h" 22#include "VectorDrawable.h" 23 24#include <SkCanvas.h> 25#include <SkTLazy.h> 26 27namespace android { 28 29// Holds an SkCanvas reference plus additional native data. 30class SkiaCanvas : public Canvas { 31public: 32 explicit SkiaCanvas(const SkBitmap& bitmap); 33 34 /** 35 * Create a new SkiaCanvas. 36 * 37 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must 38 * not be NULL. This constructor will ref() the SkCanvas, and unref() 39 * it in its destructor. 40 */ 41 explicit SkiaCanvas(SkCanvas* canvas); 42 43 virtual SkCanvas* asSkCanvas() override { 44 return mCanvas.get(); 45 } 46 47 virtual void resetRecording(int width, int height, 48 uirenderer::RenderNode* renderNode) override { 49 LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas"); 50 } 51 52 virtual uirenderer::DisplayList* finishRecording() override { 53 LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList"); 54 return nullptr; 55 } 56 virtual void insertReorderBarrier(bool enableReorder) override { 57 LOG_ALWAYS_FATAL("SkiaCanvas does not support reordering barriers"); 58 } 59 60 virtual void setBitmap(const SkBitmap& bitmap) override; 61 62 virtual bool isOpaque() override; 63 virtual int width() override; 64 virtual int height() override; 65 66 virtual void setHighContrastText(bool highContrastText) override { 67 mHighContrastText = highContrastText; 68 } 69 virtual bool isHighContrastText() override { return mHighContrastText; } 70 71 virtual int getSaveCount() const override; 72 virtual int save(SaveFlags::Flags flags) override; 73 virtual void restore() override; 74 virtual void restoreToCount(int saveCount) override; 75 76 virtual int saveLayer(float left, float top, float right, float bottom, 77 const SkPaint* paint, SaveFlags::Flags flags) override; 78 virtual int saveLayerAlpha(float left, float top, float right, float bottom, 79 int alpha, SaveFlags::Flags flags) override; 80 81 virtual void getMatrix(SkMatrix* outMatrix) const override; 82 virtual void setMatrix(const SkMatrix& matrix) override; 83 virtual void concat(const SkMatrix& matrix) override; 84 virtual void rotate(float degrees) override; 85 virtual void scale(float sx, float sy) override; 86 virtual void skew(float sx, float sy) override; 87 virtual void translate(float dx, float dy) override; 88 89 virtual bool getClipBounds(SkRect* outRect) const override; 90 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override; 91 virtual bool quickRejectPath(const SkPath& path) const override; 92 virtual bool clipRect(float left, float top, float right, float bottom, 93 SkRegion::Op op) override; 94 virtual bool clipPath(const SkPath* path, SkRegion::Op op) override; 95 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override; 96 97 virtual SkDrawFilter* getDrawFilter() override; 98 virtual void setDrawFilter(SkDrawFilter* drawFilter) override; 99 100 virtual void drawColor(int color, SkBlendMode mode) override; 101 virtual void drawPaint(const SkPaint& paint) override; 102 103 virtual void drawPoint(float x, float y, const SkPaint& paint) override; 104 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override; 105 virtual void drawLine(float startX, float startY, float stopX, float stopY, 106 const SkPaint& paint) override; 107 virtual void drawLines(const float* points, int count, const SkPaint& paint) override; 108 virtual void drawRect(float left, float top, float right, float bottom, 109 const SkPaint& paint) override; 110 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override; 111 virtual void drawRoundRect(float left, float top, float right, float bottom, 112 float rx, float ry, const SkPaint& paint) override; 113 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override; 114 virtual void drawOval(float left, float top, float right, float bottom, 115 const SkPaint& paint) override; 116 virtual void drawArc(float left, float top, float right, float bottom, 117 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override; 118 virtual void drawPath(const SkPath& path, const SkPaint& paint) override; 119 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount, 120 const float* verts, const float* tex, const int* colors, 121 const uint16_t* indices, int indexCount, const SkPaint& paint) override; 122 123 virtual void drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) override; 124 virtual void drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) override; 125 virtual void drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, 126 float srcRight, float srcBottom, float dstLeft, float dstTop, 127 float dstRight, float dstBottom, const SkPaint* paint) override; 128 virtual void drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight, 129 const float* vertices, const int* colors, const SkPaint* paint) override; 130 virtual void drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& chunk, 131 float dstLeft, float dstTop, float dstRight, float dstBottom, 132 const SkPaint* paint) override; 133 134 virtual bool drawTextAbsolutePos() const override { return true; } 135 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override; 136 137 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, 138 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right, 139 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx, 140 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) override; 141 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, 142 uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius, 143 uirenderer::CanvasPropertyPaint* paint) override; 144 145 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override; 146 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override; 147 virtual void callDrawGLFunction(Functor* functor, 148 uirenderer::GlFunctorLifecycleListener* listener) override; 149 150protected: 151 SkiaCanvas(); 152 void reset(SkCanvas* skiaCanvas); 153 void drawDrawable(SkDrawable* drawable) { mCanvas->drawDrawable(drawable); } 154 155 virtual void drawGlyphs(const uint16_t* text, const float* positions, int count, 156 const SkPaint& paint, float x, float y, 157 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom, 158 float totalAdvance) override; 159 virtual void drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, 160 const SkPaint& paint, const SkPath& path, size_t start, size_t end) override; 161 162private: 163 struct SaveRec { 164 int saveCount; 165 SaveFlags::Flags saveFlags; 166 size_t clipIndex; 167 }; 168 169 bool mHighContrastText = false; 170 171 const SaveRec* currentSaveRec() const; 172 void recordPartialSave(SaveFlags::Flags flags); 173 174 template<typename T> 175 void recordClip(const T&, SkRegion::Op); 176 void applyPersistentClips(size_t clipStartIndex); 177 178 void drawPoints(const float* points, int count, const SkPaint& paint, 179 SkCanvas::PointMode mode); 180 181 class Clip; 182 183 sk_sp<SkCanvas> mCanvas; 184 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves. 185 std::vector<Clip> mClipStack; // tracks persistent clips. 186}; 187 188} // namespace android 189 190