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