OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/compositor/paint_recorder.h" | 5 #include "ui/compositor/paint_recorder.h" |
6 | 6 |
7 #include "cc/resources/display_item_list.h" | 7 #include "cc/resources/display_item_list.h" |
8 #include "cc/resources/drawing_display_item.h" | 8 #include "cc/resources/drawing_display_item.h" |
9 #include "third_party/skia/include/core/SkPictureRecorder.h" | 9 #include "third_party/skia/include/core/SkPictureRecorder.h" |
10 #include "ui/compositor/paint_cache.h" | 10 #include "ui/compositor/paint_cache.h" |
11 #include "ui/compositor/paint_context.h" | 11 #include "ui/compositor/paint_context.h" |
12 #include "ui/gfx/canvas.h" | |
13 #include "ui/gfx/skia_util.h" | 12 #include "ui/gfx/skia_util.h" |
14 | 13 |
15 namespace ui { | 14 namespace ui { |
16 | 15 |
17 PaintRecorder::PaintRecorder(const PaintContext& context, PaintCache* cache) | 16 PaintRecorder::PaintRecorder(const PaintContext& context, PaintCache* cache) |
18 : context_(context), canvas_(context.canvas_), cache_(cache) { | 17 : context_(context), |
| 18 owned_canvas_( |
| 19 // If the |context| has a canvas, we'll just point to it in |canvas_| |
| 20 // so use anything here to initialize the gfx::Canvas without any |
| 21 // allocations. |
| 22 // The SkCanvas reference returned by beginRecording is shared with |
| 23 // the recorder_ so no need to store a RefPtr to it on this class. |
| 24 (context.canvas_ |
| 25 ? context.canvas_->sk_canvas() |
| 26 : skia::SharePtr( |
| 27 context.recorder_->beginRecording( |
| 28 gfx::RectToSkRect(context.bounds_), |
| 29 nullptr /* no SkRTreeFactory */, |
| 30 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag)) |
| 31 .get()), |
| 32 context.device_scale_factor_), |
| 33 // In the case that we use a shared canvas from PaintContext, the user of |
| 34 // PaintRecorder is expected to Save/Restore any changes made to the |
| 35 // context while using PaintRecorder. |
| 36 canvas_(context.canvas_ ? context.canvas_ : &owned_canvas_), |
| 37 cache_(cache) { |
19 #if DCHECK_IS_ON() | 38 #if DCHECK_IS_ON() |
20 DCHECK(!context.inside_paint_recorder_); | 39 DCHECK(!context.inside_paint_recorder_); |
21 context.inside_paint_recorder_ = true; | 40 context.inside_paint_recorder_ = true; |
22 #endif | 41 #endif |
23 | |
24 if (context.list_) { | |
25 SkRTreeFactory* no_factory = nullptr; | |
26 // This SkCancas is shared with the recorder_ so no need to store a RefPtr | |
27 // to it on this class. | |
28 skia::RefPtr<SkCanvas> skcanvas = | |
29 skia::SharePtr(context.recorder_->beginRecording( | |
30 gfx::RectToSkRect(context.bounds_), no_factory, | |
31 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag)); | |
32 owned_canvas_ = make_scoped_ptr(gfx::Canvas::CreateCanvasWithoutScaling( | |
33 skcanvas.get(), context.device_scale_factor_)); | |
34 canvas_ = owned_canvas_.get(); | |
35 } | |
36 } | 42 } |
37 | 43 |
38 PaintRecorder::PaintRecorder(const PaintContext& context) | 44 PaintRecorder::PaintRecorder(const PaintContext& context) |
39 : PaintRecorder(context, nullptr) { | 45 : PaintRecorder(context, nullptr) { |
40 } | 46 } |
41 | 47 |
42 PaintRecorder::~PaintRecorder() { | 48 PaintRecorder::~PaintRecorder() { |
43 #if DCHECK_IS_ON() | 49 #if DCHECK_IS_ON() |
44 context_.inside_paint_recorder_ = false; | 50 context_.inside_paint_recorder_ = false; |
45 #endif | 51 #endif |
46 | 52 |
47 if (!context_.list_) | 53 if (!context_.list_) |
48 return; | 54 return; |
49 | 55 |
50 auto* item = context_.list_->CreateAndAppendItem<cc::DrawingDisplayItem>(); | 56 auto* item = context_.list_->CreateAndAppendItem<cc::DrawingDisplayItem>(); |
51 item->SetNew(skia::AdoptRef(context_.recorder_->endRecordingAsPicture())); | 57 item->SetNew(skia::AdoptRef(context_.recorder_->endRecordingAsPicture())); |
52 if (cache_) | 58 if (cache_) |
53 cache_->SetCache(item); | 59 cache_->SetCache(item); |
54 } | 60 } |
55 | 61 |
56 } // namespace ui | 62 } // namespace ui |
OLD | NEW |