picture.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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 26namespace base { 27class Value; 28} 29 30namespace skia { 31class AnalysisCanvas; 32} 33 34namespace cc { 35 36class ContentLayerClient; 37 38class CC_EXPORT Picture 39 : public base::RefCountedThreadSafe<Picture> { 40 public: 41 typedef std::pair<int, int> PixelRefMapKey; 42 typedef std::vector<skia::LazyPixelRef*> PixelRefs; 43 typedef base::hash_map<PixelRefMapKey, PixelRefs> PixelRefMap; 44 45 static scoped_refptr<Picture> Create(gfx::Rect layer_rect); 46 static scoped_refptr<Picture> CreateFromValue(const base::Value* value); 47 static scoped_refptr<Picture> CreateFromSkpValue(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 64 // Gather pixel refs from recording. 65 void GatherPixelRefs(const SkTileGridPicture::TileGridInfo& tile_grid_info); 66 67 // Has Record() been called yet? 68 bool HasRecording() const { return picture_.get() != NULL; } 69 70 // Apply this contents scale and raster the content rect into the canvas. 71 int Raster(SkCanvas* canvas, 72 SkDrawPictureCallback* callback, 73 gfx::Rect content_rect, 74 float contents_scale); 75 76 // Draw the picture directly into the given canvas, without applying any 77 // clip/scale/layer transformations. 78 void Replay(SkCanvas* canvas); 79 80 scoped_ptr<base::Value> AsValue() const; 81 82 class CC_EXPORT PixelRefIterator { 83 public: 84 PixelRefIterator(); 85 PixelRefIterator(gfx::Rect layer_rect, const Picture* picture); 86 ~PixelRefIterator(); 87 88 skia::LazyPixelRef* operator->() const { 89 DCHECK_LT(current_index_, current_pixel_refs_->size()); 90 return (*current_pixel_refs_)[current_index_]; 91 } 92 93 skia::LazyPixelRef* operator*() const { 94 DCHECK_LT(current_index_, current_pixel_refs_->size()); 95 return (*current_pixel_refs_)[current_index_]; 96 } 97 98 PixelRefIterator& operator++(); 99 operator bool() const { 100 return current_index_ < current_pixel_refs_->size(); 101 } 102 103 private: 104 static base::LazyInstance<PixelRefs> empty_pixel_refs_; 105 const Picture* picture_; 106 const PixelRefs* current_pixel_refs_; 107 unsigned current_index_; 108 109 gfx::Point min_point_; 110 gfx::Point max_point_; 111 int current_x_; 112 int current_y_; 113 }; 114 115 void EmitTraceSnapshot(); 116 void EmitTraceSnapshotAlias(Picture* original); 117 118 private: 119 explicit Picture(gfx::Rect layer_rect); 120 // This constructor assumes SkPicture is already ref'd and transfers 121 // ownership to this picture. 122 Picture(const skia::RefPtr<SkPicture>&, 123 gfx::Rect layer_rect, 124 gfx::Rect opaque_rect, 125 const PixelRefMap& pixel_refs); 126 // This constructor will call AdoptRef on the SkPicture. 127 Picture(SkPicture*, 128 gfx::Rect layer_rect, 129 gfx::Rect opaque_rect); 130 ~Picture(); 131 132 gfx::Rect layer_rect_; 133 gfx::Rect opaque_rect_; 134 skia::RefPtr<SkPicture> picture_; 135 136 typedef std::vector<scoped_refptr<Picture> > PictureVector; 137 PictureVector clones_; 138 139 PixelRefMap pixel_refs_; 140 gfx::Point min_pixel_cell_; 141 gfx::Point max_pixel_cell_; 142 gfx::Size cell_size_; 143 144 scoped_refptr<base::debug::ConvertableToTraceFormat> 145 AsTraceableRasterData(gfx::Rect rect, float scale) const; 146 scoped_refptr<base::debug::ConvertableToTraceFormat> 147 AsTraceableRecordData() const; 148 149 friend class base::RefCountedThreadSafe<Picture>; 150 friend class PixelRefIterator; 151 DISALLOW_COPY_AND_ASSIGN(Picture); 152}; 153 154} // namespace cc 155 156#endif // CC_RESOURCES_PICTURE_H_ 157