summaryrefslogtreecommitdiffstats
path: root/printing/metafile_skia_wrapper.cc
blob: cc7fa0880417fac2f3dfc066bfae97f04d72e272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "printing/metafile_skia_wrapper.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkDevice.h"
#include "third_party/skia/include/core/SkMetaData.h"

namespace printing {

namespace {

const char* kDraftModeKey = "CrDraftMode";
const char* kMetafileKey = "CrMetafile";

SkMetaData& getMetaData(SkCanvas* canvas) {
  DCHECK(canvas != NULL);

  SkDevice* device = canvas->getDevice();
  DCHECK(device != NULL);
  return device->getMetaData();
}

}  // namespace

// static
void MetafileSkiaWrapper::SetMetafileOnCanvas(SkCanvas* canvas,
                                              Metafile* metafile) {
  MetafileSkiaWrapper* wrapper = NULL;
  if (metafile)
    wrapper = new MetafileSkiaWrapper(metafile);

  SkMetaData& meta = getMetaData(canvas);
  meta.setRefCnt(kMetafileKey, wrapper);
  SkSafeUnref(wrapper);
}

// static
Metafile* MetafileSkiaWrapper::GetMetafileFromCanvas(SkCanvas* canvas) {
  SkMetaData& meta = getMetaData(canvas);
  SkRefCnt* value;
  if (!meta.findRefCnt(kMetafileKey, &value) || !value)
    return NULL;

  return static_cast<MetafileSkiaWrapper*>(value)->metafile_;
}

// static
void MetafileSkiaWrapper::SetDraftMode(SkCanvas* canvas, bool draft_mode) {
  SkMetaData& meta = getMetaData(canvas);
  meta.setBool(kDraftModeKey, draft_mode);
}

// static
bool MetafileSkiaWrapper::GetDraftMode(SkCanvas* canvas) {
  SkMetaData& meta = getMetaData(canvas);
  bool draft_mode;
  if (!meta.findBool(kDraftModeKey, &draft_mode))
    draft_mode = false;
  return draft_mode;
}

MetafileSkiaWrapper::MetafileSkiaWrapper(Metafile* metafile)
    : metafile_(metafile) {
}

}  // namespace printing