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_H_ 6 #define CC_RESOURCES_PICTURE_H_ 7 8 #include <string> 9 #include <utility> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/containers/hash_tables.h" 14 #include "base/debug/trace_event.h" 15 #include "base/lazy_instance.h" 16 #include "base/logging.h" 17 #include "base/memory/ref_counted.h" 18 #include "base/memory/scoped_ptr.h" 19 #include "cc/base/cc_export.h" 20 #include "skia/ext/lazy_pixel_ref.h" 21 #include "skia/ext/refptr.h" 22 #include "third_party/skia/include/core/SkPixelRef.h" 23 #include "third_party/skia/include/core/SkTileGridPicture.h" 24 #include "ui/gfx/rect.h" 25 26 namespace base { 27 class Value; 28 } 29 30 namespace skia { 31 class AnalysisCanvas; 32 } 33 34 namespace cc { 35 36 class ContentLayerClient; 37 class RenderingStatsInstrumentation; 38 39 class CC_EXPORT Picture 40 : public base::RefCountedThreadSafe<Picture> { 41 public: 42 typedef std::pair<int, int> PixelRefMapKey; 43 typedef std::vector<skia::LazyPixelRef*> PixelRefs; 44 typedef base::hash_map<PixelRefMapKey, PixelRefs> PixelRefMap; 45 46 static scoped_refptr<Picture> Create(gfx::Rect layer_rect); 47 static scoped_refptr<Picture> CreateFromValue(const base::Value* value); 48 49 gfx::Rect LayerRect() const { return layer_rect_; } 50 gfx::Rect OpaqueRect() const { return opaque_rect_; } 51 52 // Get thread-safe clone for rasterizing with on a specific thread. 53 scoped_refptr<Picture> GetCloneForDrawingOnThread( 54 unsigned thread_index) const; 55 56 // Make thread-safe clones for rasterizing with. 57 void CloneForDrawing(int num_threads); 58 59 // Record a paint operation. To be able to safely use this SkPicture for 60 // playback on a different thread this can only be called once. 61 void Record(ContentLayerClient* client, 62 const SkTileGridPicture::TileGridInfo& tile_grid_info, 63 RenderingStatsInstrumentation* stats_instrumentation); 64 65 // Gather pixel refs from recording. 66 void GatherPixelRefs(const SkTileGridPicture::TileGridInfo& tile_grid_info, 67 RenderingStatsInstrumentation* stats_instrumentation); 68 69 // Has Record() been called yet? 70 bool HasRecording() const { return picture_.get() != NULL; } 71 72 // Apply this contents scale and raster the content rect into the canvas. 73 void Raster(SkCanvas* canvas, 74 SkDrawPictureCallback* callback, 75 gfx::Rect content_rect, 76 float contents_scale); 77 78 // Draw the picture directly into the given canvas, without applying any 79 // clip/scale/layer transformations. 80 void Replay(SkCanvas* canvas); 81 82 scoped_ptr<base::Value> AsValue() const; 83 84 class CC_EXPORT PixelRefIterator { 85 public: 86 PixelRefIterator(); 87 PixelRefIterator(gfx::Rect layer_rect, const Picture* picture); 88 ~PixelRefIterator(); 89 90 skia::LazyPixelRef* operator->() const { 91 DCHECK_LT(current_index_, current_pixel_refs_->size()); 92 return (*current_pixel_refs_)[current_index_]; 93 } 94 95 skia::LazyPixelRef* operator*() const { 96 DCHECK_LT(current_index_, current_pixel_refs_->size()); 97 return (*current_pixel_refs_)[current_index_]; 98 } 99 100 PixelRefIterator& operator++(); 101 operator bool() const { 102 return current_index_ < current_pixel_refs_->size(); 103 } 104 105 private: 106 static base::LazyInstance<PixelRefs> empty_pixel_refs_; 107 const Picture* picture_; 108 const PixelRefs* current_pixel_refs_; 109 unsigned current_index_; 110 111 gfx::Point min_point_; 112 gfx::Point max_point_; 113 int current_x_; 114 int current_y_; 115 }; 116 117 void EmitTraceSnapshot(); 118 119 private: 120 explicit Picture(gfx::Rect layer_rect); 121 // This constructor assumes SkPicture is already ref'd and transfers 122 // ownership to this picture. 123 Picture(const skia::RefPtr<SkPicture>&, 124 gfx::Rect layer_rect, 125 gfx::Rect opaque_rect, 126 const PixelRefMap& pixel_refs); 127 // This constructor will call AdoptRef on the SkPicture. 128 Picture(SkPicture*, 129 gfx::Rect layer_rect, 130 gfx::Rect opaque_rect); 131 ~Picture(); 132 133 gfx::Rect layer_rect_; 134 gfx::Rect opaque_rect_; 135 skia::RefPtr<SkPicture> picture_; 136 137 typedef std::vector<scoped_refptr<Picture> > PictureVector; 138 PictureVector clones_; 139 140 PixelRefMap pixel_refs_; 141 gfx::Point min_pixel_cell_; 142 gfx::Point max_pixel_cell_; 143 gfx::Size cell_size_; 144 145 scoped_ptr<base::debug::ConvertableToTraceFormat> 146 AsTraceableRasterData(gfx::Rect rect, float scale) const; 147 scoped_ptr<base::debug::ConvertableToTraceFormat> 148 AsTraceableRecordData() const; 149 150 friend class base::RefCountedThreadSafe<Picture>; 151 friend class PixelRefIterator; 152 DISALLOW_COPY_AND_ASSIGN(Picture); 153 }; 154 155 } // namespace cc 156 157 #endif // CC_RESOURCES_PICTURE_H_ 158