summaryrefslogtreecommitdiffstats
path: root/webkit/glue/plugins/pepper_buffer.cc
blob: 1c0bdd87354e8646d0a09a6d920a307c18456322 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2010 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 "webkit/glue/plugins/pepper_buffer.h"

#include <algorithm>

#include "base/logging.h"
#include "base/scoped_ptr.h"
#include "third_party/ppapi/c/pp_instance.h"
#include "third_party/ppapi/c/pp_module.h"
#include "third_party/ppapi/c/pp_resource.h"
#include "third_party/ppapi/c/ppb_buffer.h"
#include "webkit/glue/plugins/pepper_plugin_instance.h"
#include "webkit/glue/plugins/pepper_plugin_module.h"

namespace pepper {

namespace {

PP_Resource Create(PP_Module module_id, int32_t size) {
  PluginModule* module = PluginModule::FromPPModule(module_id);
  if (!module)
    return NULL;

  scoped_refptr<Buffer> buffer(new Buffer(module));
  if (!buffer->Init(size))
    return NULL;

  return buffer->GetReference();
}

bool IsBuffer(PP_Resource resource) {
  return !!Resource::GetAs<Buffer>(resource);
}

bool Describe(PP_Resource resource, int32_t* size_in_bytes) {
  scoped_refptr<Buffer> buffer(Resource::GetAs<Buffer>(resource));
  if (!buffer)
    return false;
  buffer->Describe(size_in_bytes);
  return true;
}

void* Map(PP_Resource resource) {
  scoped_refptr<Buffer> buffer(Resource::GetAs<Buffer>(resource));
  if (!buffer)
    return NULL;
  return buffer->Map();
}

void Unmap(PP_Resource resource) {
  scoped_refptr<Buffer> buffer(Resource::GetAs<Buffer>(resource));
  if (!buffer)
    return;
  return buffer->Unmap();
}

const PPB_Buffer ppb_buffer = {
  &Create,
  &IsBuffer,
  &Describe,
  &Map,
  &Unmap,
};

}  // namespace

Buffer::Buffer(PluginModule* module)
    : Resource(module),
      size_(0) {
}

Buffer::~Buffer() {
}

// static
const PPB_Buffer* Buffer::GetInterface() {
  return &ppb_buffer;
}

bool Buffer::Init(int size) {
  if (size == 0)
    return false;
  Unmap();
  size_ = size;
  return true;
}

void Buffer::Describe(int* size_in_bytes) const {
  *size_in_bytes = size_;
}

void* Buffer::Map() {
  if (size_ == 0)
    return NULL;

  if (!is_mapped()) {
    mem_buffer_.reset(new unsigned char[size_]);
    memset(mem_buffer_.get(), 0, size_);
  }
  return mem_buffer_.get();
}

void Buffer::Unmap() {
  mem_buffer_.reset();
}

void Buffer::Swap(Buffer* other) {
  swap(other->mem_buffer_, mem_buffer_);
  std::swap(other->size_, size_);
}

}  // namespace pepper