summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppb_image_data_proxy.cc
blob: 4fa4205ed415ece10b68e044885064945109c486 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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 "ppapi/proxy/ppb_image_data_proxy.h"

#include <string.h>  // For memcpy

#include <vector>

#include "base/logging.h"
#include "build/build_config.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/trusted/ppb_image_data_trusted.h"
#include "ppapi/proxy/image_data.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/ppapi_messages.h"

namespace pp {
namespace proxy {

namespace {

PP_ImageDataFormat GetNativeImageDataFormat() {
  int32 format = 0;
  PluginDispatcher::Get()->Send(
      new PpapiHostMsg_PPBImageData_GetNativeImageDataFormat(
          INTERFACE_ID_PPB_IMAGE_DATA, &format));
  return static_cast<PP_ImageDataFormat>(format);
}

PP_Bool IsImageDataFormatSupported(PP_ImageDataFormat format) {
  PP_Bool supported = PP_FALSE;
  PluginDispatcher::Get()->Send(
      new PpapiHostMsg_PPBImageData_IsImageDataFormatSupported(
          INTERFACE_ID_PPB_IMAGE_DATA, static_cast<int32_t>(format),
          &supported));
  return supported;
}

PP_Resource Create(PP_Module module_id,
                   PP_ImageDataFormat format,
                   const PP_Size* size,
                   PP_Bool init_to_zero) {
  PP_Resource result = 0;
  std::string image_data_desc;
  ImageHandle image_handle = ImageData::NullHandle;
  PluginDispatcher::Get()->Send(
      new PpapiHostMsg_PPBImageData_Create(
          INTERFACE_ID_PPB_IMAGE_DATA, module_id, format, *size, init_to_zero,
          &result, &image_data_desc, &image_handle));

  if (result && image_data_desc.size() == sizeof(PP_ImageDataDesc)) {
    // We serialize the PP_ImageDataDesc just by copying to a string.
    PP_ImageDataDesc desc;
    memcpy(&desc, image_data_desc.data(), sizeof(PP_ImageDataDesc));

    linked_ptr<ImageData> object(
        new ImageData(desc, image_handle));
    PluginDispatcher::Get()->plugin_resource_tracker()->AddResource(
        result, object);
  }
  return result;
}

PP_Bool IsImageData(PP_Resource resource) {
  ImageData* object = PluginResource::GetAs<ImageData>(resource);
  return BoolToPPBool(!!object);
}

PP_Bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) {
  ImageData* object = PluginResource::GetAs<ImageData>(resource);
  if (!object)
    return PP_FALSE;
  memcpy(desc, &object->desc(), sizeof(PP_ImageDataDesc));
  return PP_TRUE;
}

void* Map(PP_Resource resource) {
  ImageData* object = PluginResource::GetAs<ImageData>(resource);
  if (!object)
    return NULL;
  return object->Map();
}

void Unmap(PP_Resource resource) {
  ImageData* object = PluginResource::GetAs<ImageData>(resource);
  if (object)
    object->Unmap();
}

const PPB_ImageData ppb_imagedata = {
  &GetNativeImageDataFormat,
  &IsImageDataFormatSupported,
  &Create,
  &IsImageData,
  &Describe,
  &Map,
  &Unmap,
};

}  // namespace

PPB_ImageData_Proxy::PPB_ImageData_Proxy(Dispatcher* dispatcher,
                                         const void* target_interface)
    : InterfaceProxy(dispatcher, target_interface) {
}

PPB_ImageData_Proxy::~PPB_ImageData_Proxy() {
}

const void* PPB_ImageData_Proxy::GetSourceInterface() const {
  return &ppb_imagedata;
}

InterfaceID PPB_ImageData_Proxy::GetInterfaceId() const {
  return INTERFACE_ID_PPB_IMAGE_DATA;
}

void PPB_ImageData_Proxy::OnMessageReceived(const IPC::Message& msg) {
  IPC_BEGIN_MESSAGE_MAP(PPB_ImageData_Proxy, msg)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_GetNativeImageDataFormat,
                        OnMsgGetNativeImageDataFormat)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_IsImageDataFormatSupported,
                        OnMsgIsImageDataFormatSupported)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_Create, OnMsgCreate)
  IPC_END_MESSAGE_MAP()
  // FIXME(brettw) handle bad messages!
}

void PPB_ImageData_Proxy::OnMsgGetNativeImageDataFormat(int32* result) {
  *result = ppb_image_data_target()->GetNativeImageDataFormat();
}

void PPB_ImageData_Proxy::OnMsgIsImageDataFormatSupported(int32 format,
                                                          PP_Bool* result) {
  *result = ppb_image_data_target()->IsImageDataFormatSupported(
      static_cast<PP_ImageDataFormat>(format));
}

void PPB_ImageData_Proxy::OnMsgCreate(PP_Module module,
                                      int32_t format,
                                      const PP_Size& size,
                                      PP_Bool init_to_zero,
                                      PP_Resource* result,
                                      std::string* image_data_desc,
                                      ImageHandle* result_image_handle) {
  *result = ppb_image_data_target()->Create(
      module, static_cast<PP_ImageDataFormat>(format), &size, init_to_zero);
  *result_image_handle = ImageData::NullHandle;
  if (*result) {
    // The ImageDesc is just serialized as a string.
    PP_ImageDataDesc desc;
    if (ppb_image_data_target()->Describe(*result, &desc)) {
      image_data_desc->resize(sizeof(PP_ImageDataDesc));
      memcpy(&(*image_data_desc)[0], &desc, sizeof(PP_ImageDataDesc));
    }

    // Get the shared memory handle.
    const PPB_ImageDataTrusted* trusted =
        reinterpret_cast<const PPB_ImageDataTrusted*>(
            dispatcher()->GetLocalInterface(PPB_IMAGEDATA_TRUSTED_INTERFACE));
    uint32_t byte_count = 0;
    if (trusted) {
      int32_t handle;
      if (trusted->GetSharedMemory(*result, &handle, &byte_count) == PP_OK)
          *result_image_handle = ImageData::HandleFromInt(handle);
    }
  }
}

}  // namespace proxy
}  // namespace pp