summaryrefslogtreecommitdiff
path: root/chromium/pdf/ppapi_migration/graphics.cc
blob: 7b426bb607d25a4775fc1bc603966b910a250fe0 (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
70
71
72
73
74
75
76
// Copyright 2020 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 "pdf/ppapi_migration/graphics.h"

#include <stdint.h>

#include <utility>

#include "base/callback.h"
#include "base/check_op.h"
#include "pdf/ppapi_migration/callback.h"
#include "pdf/ppapi_migration/geometry_conversions.h"
#include "pdf/ppapi_migration/image.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/point.h"
#include "ppapi/cpp/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h"

namespace chrome_pdf {

Graphics::Graphics(const gfx::Size& size) : size_(size) {}

PepperGraphics::PepperGraphics(const pp::InstanceHandle& instance,
                               const gfx::Size& size)
    : Graphics(size),
      pepper_graphics_(instance,
                       PPSizeFromSize(size),
                       /*is_always_opaque=*/true) {}

PepperGraphics::~PepperGraphics() = default;

bool PepperGraphics::Flush(ResultCallback callback) {
  pp::CompletionCallback pp_callback =
      PPCompletionCallbackFromResultCallback(std::move(callback));
  int32_t result = pepper_graphics_.Flush(pp_callback);
  if (result == PP_OK_COMPLETIONPENDING) {
    return true;
  }

  // Should only happen if pp::Graphics2D::Flush() is called while a callback is
  // still pending, which should never happen if PaintManager is managing all
  // flushes.
  DCHECK_EQ(PP_OK, result);
  pp_callback.Run(result);
  return false;
}

void PepperGraphics::PaintImage(const Image& image, const gfx::Rect& src_rect) {
  pepper_graphics_.PaintImageData(image.pepper_image(), pp::Point(),
                                  PPRectFromRect(src_rect));
}

void PepperGraphics::Scroll(const gfx::Rect& clip,
                            const gfx::Vector2d& amount) {
  pepper_graphics_.Scroll(PPRectFromRect(clip), PPPointFromVector(amount));
}

void PepperGraphics::SetScale(float scale) {
  bool result = pepper_graphics_.SetScale(scale);
  DCHECK(result);
}

void PepperGraphics::SetLayerTransform(float scale,
                                       const gfx::Point& origin,
                                       const gfx::Vector2d& translate) {
  bool result = pepper_graphics_.SetLayerTransform(
      scale, PPPointFromPoint(origin), PPPointFromVector(translate));
  DCHECK(result);
}

}  // namespace chrome_pdf