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