ui: Eliminate allocating gfx::Canvas on the heap for every view.
[chromium-blink-merge.git] / ui / compositor / paint_recorder.cc
blobe5de0c2d878376b898b0a09aca6d0c86a1145e3f
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"
14 namespace ui {
16 PaintRecorder::PaintRecorder(const PaintContext& context, PaintCache* 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) {
38 #if DCHECK_IS_ON()
39 DCHECK(!context.inside_paint_recorder_);
40 context.inside_paint_recorder_ = true;
41 #endif
44 PaintRecorder::PaintRecorder(const PaintContext& context)
45 : PaintRecorder(context, nullptr) {
48 PaintRecorder::~PaintRecorder() {
49 #if DCHECK_IS_ON()
50 context_.inside_paint_recorder_ = false;
51 #endif
53 if (!context_.list_)
54 return;
56 auto* item = context_.list_->CreateAndAppendItem<cc::DrawingDisplayItem>();
57 item->SetNew(skia::AdoptRef(context_.recorder_->endRecordingAsPicture()));
58 if (cache_)
59 cache_->SetCache(item);
62 } // namespace ui