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