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