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
3 // found in the LICENSE file.
5 #include "ui/compositor/paint_recorder.h"
7 #include "cc/resources/display_item_list.h"
8 #include "cc/resources/drawing_display_item.h"
9 #include "third_party/skia/include/core/SkPictureRecorder.h"
10 #include "ui/compositor/paint_cache.h"
11 #include "ui/compositor/paint_context.h"
12 #include "ui/gfx/skia_util.h"
16 PaintRecorder::PaintRecorder(const PaintContext
& context
, PaintCache
* cache
)
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
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.
25 ? context
.canvas_
->sk_canvas()
27 context
.recorder_
->beginRecording(
28 gfx::RectToSkRect(context
.bounds_
),
29 nullptr /* no SkRTreeFactory */,
30 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag
))
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_
),
39 DCHECK(!context
.inside_paint_recorder_
);
40 context
.inside_paint_recorder_
= true;
44 PaintRecorder::PaintRecorder(const PaintContext
& context
)
45 : PaintRecorder(context
, nullptr) {
48 PaintRecorder::~PaintRecorder() {
50 context_
.inside_paint_recorder_
= false;
56 auto* item
= context_
.list_
->CreateAndAppendItem
<cc::DrawingDisplayItem
>();
57 item
->SetNew(skia::AdoptRef(context_
.recorder_
->endRecordingAsPicture()));
59 cache_
->SetCache(item
);