RecordingCanvas.h revision f276acd98457bcaabc9e79a17a736b3b484f005e
1/* 2 * Copyright (C) 2015 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#ifndef ANDROID_HWUI_RECORDING_CANVAS_H 18#define ANDROID_HWUI_RECORDING_CANVAS_H 19 20#include "Canvas.h" 21#include "CanvasState.h" 22#include "DisplayList.h" 23#include "ResourceCache.h" 24#include "SkiaCanvasProxy.h" 25#include "Snapshot.h" 26#include "utils/LinearAllocator.h" 27#include "utils/Macros.h" 28#include "utils/NinePatch.h" 29 30#include <SkDrawFilter.h> 31#include <SkPaint.h> 32#include <SkTLazy.h> 33 34#include <vector> 35 36namespace android { 37namespace uirenderer { 38 39struct ClipBase; 40class DeferredLayerUpdater; 41struct RecordedOp; 42 43class ANDROID_API RecordingCanvas: public Canvas, public CanvasStateClient { 44 enum class DeferredBarrierType { 45 None, 46 InOrder, 47 OutOfOrder, 48 }; 49public: 50 RecordingCanvas(size_t width, size_t height); 51 virtual ~RecordingCanvas(); 52 53 void reset(int width, int height); 54 WARN_UNUSED_RESULT DisplayList* finishRecording(); 55 56// ---------------------------------------------------------------------------- 57// MISC HWUI OPERATIONS - TODO: CATEGORIZE 58// ---------------------------------------------------------------------------- 59 void insertReorderBarrier(bool enableReorder) { 60 mDeferredBarrierType = enableReorder 61 ? DeferredBarrierType::OutOfOrder : DeferredBarrierType::InOrder; 62 } 63 64 void drawLayer(DeferredLayerUpdater* layerHandle); 65 void drawRenderNode(RenderNode* renderNode); 66 67 // TODO: rename for consistency 68 void callDrawGLFunction(Functor* functor); 69 70// ---------------------------------------------------------------------------- 71// CanvasStateClient interface 72// ---------------------------------------------------------------------------- 73 virtual void onViewportInitialized() override; 74 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override; 75 virtual GLuint getTargetFbo() const override { return -1; } 76 77// ---------------------------------------------------------------------------- 78// HWUI Canvas draw operations 79// ---------------------------------------------------------------------------- 80 81 void drawRoundRect(CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top, 82 CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom, 83 CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry, 84 CanvasPropertyPaint* paint); 85 void drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y, 86 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint); 87 88// ---------------------------------------------------------------------------- 89// android/graphics/Canvas interface 90// ---------------------------------------------------------------------------- 91 virtual SkCanvas* asSkCanvas() override; 92 93 virtual void setBitmap(const SkBitmap& bitmap) override { 94 LOG_ALWAYS_FATAL("RecordingCanvas is not backed by a bitmap."); 95 } 96 97 virtual bool isOpaque() override { return false; } 98 virtual int width() override { return mState.getWidth(); } 99 virtual int height() override { return mState.getHeight(); } 100 101 virtual void setHighContrastText(bool highContrastText) override { 102 mHighContrastText = highContrastText; 103 } 104 virtual bool isHighContrastText() override { return mHighContrastText; } 105 106// ---------------------------------------------------------------------------- 107// android/graphics/Canvas state operations 108// ---------------------------------------------------------------------------- 109 // Save (layer) 110 virtual int getSaveCount() const override { return mState.getSaveCount(); } 111 virtual int save(SaveFlags::Flags flags) override; 112 virtual void restore() override; 113 virtual void restoreToCount(int saveCount) override; 114 115 virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint, 116 SaveFlags::Flags flags) override; 117 virtual int saveLayerAlpha(float left, float top, float right, float bottom, 118 int alpha, SaveFlags::Flags flags) override { 119 SkPaint paint; 120 paint.setAlpha(alpha); 121 return saveLayer(left, top, right, bottom, &paint, flags); 122 } 123 124 // Matrix 125 virtual void getMatrix(SkMatrix* outMatrix) const override { mState.getMatrix(outMatrix); } 126 virtual void setMatrix(const SkMatrix& matrix) override { mState.setMatrix(matrix); } 127 128 virtual void concat(const SkMatrix& matrix) override { mState.concatMatrix(matrix); } 129 virtual void rotate(float degrees) override; 130 virtual void scale(float sx, float sy) override; 131 virtual void skew(float sx, float sy) override; 132 virtual void translate(float dx, float dy) override; 133 134 // Clip 135 virtual bool getClipBounds(SkRect* outRect) const override; 136 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override; 137 virtual bool quickRejectPath(const SkPath& path) const override; 138 139 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) override; 140 virtual bool clipPath(const SkPath* path, SkRegion::Op op) override; 141 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override; 142 143 // Misc 144 virtual SkDrawFilter* getDrawFilter() override { return mDrawFilter.get(); } 145 virtual void setDrawFilter(SkDrawFilter* filter) override { 146 mDrawFilter.reset(SkSafeRef(filter)); 147 } 148 149// ---------------------------------------------------------------------------- 150// android/graphics/Canvas draw operations 151// ---------------------------------------------------------------------------- 152 virtual void drawColor(int color, SkXfermode::Mode mode) override; 153 virtual void drawPaint(const SkPaint& paint) override; 154 155 // Geometry 156 virtual void drawPoint(float x, float y, const SkPaint& paint) override { 157 float points[2] = { x, y }; 158 drawPoints(points, 2, paint); 159 } 160 virtual void drawPoints(const float* points, int floatCount, const SkPaint& paint) override; 161 virtual void drawLine(float startX, float startY, float stopX, float stopY, 162 const SkPaint& paint) override { 163 float points[4] = { startX, startY, stopX, stopY }; 164 drawLines(points, 4, paint); 165 } 166 virtual void drawLines(const float* points, int floatCount, const SkPaint& paint) override; 167 virtual void drawRect(float left, float top, float right, float bottom, const SkPaint& paint) override; 168 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override; 169 virtual void drawRoundRect(float left, float top, float right, float bottom, 170 float rx, float ry, const SkPaint& paint) override; 171 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override; 172 virtual void drawOval(float left, float top, float right, float bottom, const SkPaint& paint) override; 173 virtual void drawArc(float left, float top, float right, float bottom, 174 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override; 175 virtual void drawPath(const SkPath& path, const SkPaint& paint) override; 176 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount, 177 const float* verts, const float* tex, const int* colors, 178 const uint16_t* indices, int indexCount, const SkPaint& paint) override 179 { /* RecordingCanvas does not support drawVertices(); ignore */ } 180 181 virtual void drawVectorDrawable(VectorDrawableRoot* tree) override; 182 183 // Bitmap-based 184 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) override; 185 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, 186 const SkPaint* paint) override; 187 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop, 188 float srcRight, float srcBottom, float dstLeft, float dstTop, 189 float dstRight, float dstBottom, const SkPaint* paint) override; 190 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight, 191 const float* vertices, const int* colors, const SkPaint* paint) override; 192 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk, 193 float dstLeft, float dstTop, float dstRight, float dstBottom, 194 const SkPaint* paint) override; 195 196 // Text 197 virtual void drawText(const uint16_t* glyphs, const float* positions, int glyphCount, 198 const SkPaint& paint, float x, float y, float boundsLeft, float boundsTop, 199 float boundsRight, float boundsBottom, float totalAdvance) override; 200 virtual void drawTextOnPath(const uint16_t* glyphs, int glyphCount, const SkPath& path, 201 float hOffset, float vOffset, const SkPaint& paint) override; 202 virtual bool drawTextAbsolutePos() const override { return false; } 203 204private: 205 const ClipBase* getRecordedClip() { 206 return mState.writableSnapshot()->mutateClipArea().serializeClip(alloc()); 207 } 208 209 void drawBitmap(const SkBitmap* bitmap, const SkPaint* paint); 210 void drawSimpleRects(const float* rects, int vertexCount, const SkPaint* paint); 211 212 213 size_t addOp(RecordedOp* op); 214// ---------------------------------------------------------------------------- 215// lazy object copy 216// ---------------------------------------------------------------------------- 217 LinearAllocator& alloc() { return mDisplayList->allocator; } 218 219 void refBitmapsInShader(const SkShader* shader); 220 221 template<class T> 222 inline const T* refBuffer(const T* srcBuffer, int32_t count) { 223 if (!srcBuffer) return nullptr; 224 225 T* dstBuffer = (T*) mDisplayList->allocator.alloc(count * sizeof(T)); 226 memcpy(dstBuffer, srcBuffer, count * sizeof(T)); 227 return dstBuffer; 228 } 229 230 inline const SkPath* refPath(const SkPath* path) { 231 if (!path) return nullptr; 232 233 // The points/verbs within the path are refcounted so this copy operation 234 // is inexpensive and maintains the generationID of the original path. 235 const SkPath* cachedPath = new SkPath(*path); 236 mDisplayList->pathResources.push_back(cachedPath); 237 return cachedPath; 238 } 239 240 /** 241 * Returns a RenderThread-safe, const copy of the SkPaint parameter passed in 242 * (with deduping based on paint hash / equality check) 243 */ 244 inline const SkPaint* refPaint(const SkPaint* paint) { 245 if (!paint) return nullptr; 246 247 // If there is a draw filter apply it here and store the modified paint 248 // so that we don't need to modify the paint every time we access it. 249 SkTLazy<SkPaint> filteredPaint; 250 if (mDrawFilter.get()) { 251 filteredPaint.set(*paint); 252 mDrawFilter->filter(filteredPaint.get(), SkDrawFilter::kPaint_Type); 253 paint = filteredPaint.get(); 254 } 255 256 // compute the hash key for the paint and check the cache. 257 const uint32_t key = paint->getHash(); 258 const SkPaint* cachedPaint = mPaintMap.valueFor(key); 259 // In the unlikely event that 2 unique paints have the same hash we do a 260 // object equality check to ensure we don't erroneously dedup them. 261 if (cachedPaint == nullptr || *cachedPaint != *paint) { 262 cachedPaint = new SkPaint(*paint); 263 mDisplayList->paints.emplace_back(cachedPaint); 264 // replaceValueFor() performs an add if the entry doesn't exist 265 mPaintMap.replaceValueFor(key, cachedPaint); 266 refBitmapsInShader(cachedPaint->getShader()); 267 } 268 269 return cachedPaint; 270 } 271 272 inline const SkRegion* refRegion(const SkRegion* region) { 273 if (!region) { 274 return region; 275 } 276 277 const SkRegion* cachedRegion = mRegionMap.valueFor(region); 278 // TODO: Add generation ID to SkRegion 279 if (cachedRegion == nullptr) { 280 std::unique_ptr<const SkRegion> copy(new SkRegion(*region)); 281 cachedRegion = copy.get(); 282 mDisplayList->regions.push_back(std::move(copy)); 283 284 // replaceValueFor() performs an add if the entry doesn't exist 285 mRegionMap.replaceValueFor(region, cachedRegion); 286 } 287 288 return cachedRegion; 289 } 290 291 inline const SkBitmap* refBitmap(const SkBitmap& bitmap) { 292 // Note that this assumes the bitmap is immutable. There are cases this won't handle 293 // correctly, such as creating the bitmap from scratch, drawing with it, changing its 294 // contents, and drawing again. The only fix would be to always copy it the first time, 295 // which doesn't seem worth the extra cycles for this unlikely case. 296 SkBitmap* localBitmap = new (alloc()) SkBitmap(bitmap); 297 alloc().autoDestroy(localBitmap); 298 mDisplayList->bitmapResources.push_back(localBitmap); 299 return localBitmap; 300 } 301 302 inline const Res_png_9patch* refPatch(const Res_png_9patch* patch) { 303 mDisplayList->patchResources.push_back(patch); 304 mResourceCache.incrementRefcount(patch); 305 return patch; 306 } 307 308 DefaultKeyedVector<uint32_t, const SkPaint*> mPaintMap; 309 DefaultKeyedVector<const SkPath*, const SkPath*> mPathMap; 310 DefaultKeyedVector<const SkRegion*, const SkRegion*> mRegionMap; 311 312 CanvasState mState; 313 std::unique_ptr<SkiaCanvasProxy> mSkiaCanvasProxy; 314 ResourceCache& mResourceCache; 315 DeferredBarrierType mDeferredBarrierType = DeferredBarrierType::None; 316 DisplayList* mDisplayList = nullptr; 317 bool mHighContrastText = false; 318 SkAutoTUnref<SkDrawFilter> mDrawFilter; 319}; // class RecordingCanvas 320 321}; // namespace uirenderer 322}; // namespace android 323 324#endif // ANDROID_HWUI_RECORDING_CANVAS_H 325