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