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