1 // Copyright 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CC_RESOURCES_PICTURE_PILE_IMPL_H_ 6 #define CC_RESOURCES_PICTURE_PILE_IMPL_H_ 7 8 #include <list> 9 #include <map> 10 #include <vector> 11 12 #include "base/time/time.h" 13 #include "cc/base/cc_export.h" 14 #include "cc/resources/picture_pile_base.h" 15 #include "skia/ext/analysis_canvas.h" 16 #include "skia/ext/refptr.h" 17 #include "third_party/skia/include/core/SkPicture.h" 18 19 namespace cc { 20 21 class CC_EXPORT PicturePileImpl : public PicturePileBase { 22 public: 23 static scoped_refptr<PicturePileImpl> Create(); 24 static scoped_refptr<PicturePileImpl> CreateFromOther( 25 const PicturePileBase* other); 26 27 // Get paint-safe version of this picture for a specific thread. 28 PicturePileImpl* GetCloneForDrawingOnThread(unsigned thread_index) const; 29 30 struct CC_EXPORT RasterStats { 31 // Minimum rasterize time from N runs 32 // N=max(1,slow-down-raster-scale-factor) 33 base::TimeDelta best_rasterize_time; 34 // Total rasterize time for all N runs 35 base::TimeDelta total_rasterize_time; 36 // Total number of pixels rasterize in all N runs 37 int64 total_pixels_rasterized; 38 }; 39 40 // Raster a subrect of this PicturePileImpl into the given canvas. 41 // It's only safe to call paint on a cloned version. It is assumed 42 // that contents_scale has already been applied to this canvas. 43 // Writes the total number of pixels rasterized and the time spent 44 // rasterizing to the stats if the respective pointer is not 45 // NULL. When slow-down-raster-scale-factor is set to a value 46 // greater than 1, the reported rasterize time is the minimum 47 // measured value over all runs. 48 void RasterDirect( 49 SkCanvas* canvas, 50 gfx::Rect canvas_rect, 51 float contents_scale, 52 RasterStats* raster_stats); 53 54 // Similar to the above RasterDirect method, but this is a convenience method 55 // for when it is known that the raster is going to an intermediate bitmap 56 // that itself will then be blended and thus that a canvas clear is required. 57 void RasterToBitmap( 58 SkCanvas* canvas, 59 gfx::Rect canvas_rect, 60 float contents_scale, 61 RasterStats* raster_stats); 62 63 // Called when analyzing a tile. We can use AnalysisCanvas as 64 // SkDrawPictureCallback, which allows us to early out from analysis. 65 void RasterForAnalysis( 66 skia::AnalysisCanvas* canvas, 67 gfx::Rect canvas_rect, 68 float contents_scale); 69 70 71 skia::RefPtr<SkPicture> GetFlattenedPicture(); 72 73 struct CC_EXPORT Analysis { 74 Analysis(); 75 ~Analysis(); 76 77 bool is_solid_color; 78 bool has_text; 79 SkColor solid_color; 80 }; 81 82 void AnalyzeInRect(gfx::Rect content_rect, 83 float contents_scale, 84 Analysis* analysis); 85 86 class CC_EXPORT PixelRefIterator { 87 public: 88 PixelRefIterator(gfx::Rect content_rect, 89 float contents_scale, 90 const PicturePileImpl* picture_pile); 91 ~PixelRefIterator(); 92 93 skia::LazyPixelRef* operator->() const { return *pixel_ref_iterator_; } 94 skia::LazyPixelRef* operator*() const { return *pixel_ref_iterator_; } 95 PixelRefIterator& operator++(); 96 operator bool() const { return pixel_ref_iterator_; } 97 98 private: 99 bool AdvanceToTileWithPictures(); 100 void AdvanceToPictureWithPixelRefs(); 101 102 const PicturePileImpl* picture_pile_; 103 gfx::Rect layer_rect_; 104 TilingData::Iterator tile_iterator_; 105 Picture::PixelRefIterator pixel_ref_iterator_; 106 const PictureList* picture_list_; 107 PictureList::const_iterator picture_list_iterator_; 108 }; 109 110 void DidBeginTracing(); 111 112 protected: 113 friend class PicturePile; 114 friend class PixelRefIterator; 115 116 PicturePileImpl(); 117 explicit PicturePileImpl(const PicturePileBase* other); 118 virtual ~PicturePileImpl(); 119 120 private: 121 class ClonesForDrawing { 122 public: 123 ClonesForDrawing(const PicturePileImpl* pile, int num_threads); 124 ~ClonesForDrawing(); 125 126 typedef std::vector<scoped_refptr<PicturePileImpl> > PicturePileVector; 127 PicturePileVector clones_; 128 }; 129 130 static scoped_refptr<PicturePileImpl> CreateCloneForDrawing( 131 const PicturePileImpl* other, unsigned thread_index); 132 133 PicturePileImpl(const PicturePileImpl* other, unsigned thread_index); 134 135 void RasterCommon( 136 SkCanvas* canvas, 137 SkDrawPictureCallback* callback, 138 gfx::Rect canvas_rect, 139 float contents_scale, 140 RasterStats* raster_stats); 141 142 // Once instantiated, |clones_for_drawing_| can't be modified. This 143 // guarantees thread-safe access during the life time of a PicturePileImpl 144 // instance. This member variable must be last so that other member 145 // variables have already been initialized and can be clonable. 146 const ClonesForDrawing clones_for_drawing_; 147 148 DISALLOW_COPY_AND_ASSIGN(PicturePileImpl); 149 }; 150 151 } // namespace cc 152 153 #endif // CC_RESOURCES_PICTURE_PILE_IMPL_H_ 154