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