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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
// Copyright (c) 2012 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 "content/renderer/pepper/ppb_image_data_impl.h"
#include <algorithm>
#include <limits>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "content/common/view_messages.h"
#include "content/renderer/pepper/common.h"
#include "content/renderer/pepper/resource_helper.h"
#include "content/renderer/render_thread_impl.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/ppb_image_data.h"
#include "ppapi/thunk/thunk.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/skia/include/core/SkColorPriv.h"
#include "ui/surface/transport_dib.h"
using ::ppapi::thunk::PPB_ImageData_API;
namespace content {
PPB_ImageData_Impl::PPB_ImageData_Impl(PP_Instance instance,
PPB_ImageData_Shared::ImageDataType type)
: Resource(::ppapi::OBJECT_IS_IMPL, instance),
format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL),
width_(0),
height_(0) {
switch (type) {
case PPB_ImageData_Shared::PLATFORM:
backend_.reset(new ImageDataPlatformBackend);
return;
case PPB_ImageData_Shared::SIMPLE:
backend_.reset(new ImageDataSimpleBackend);
return;
// No default: so that we get a compiler warning if any types are added.
}
NOTREACHED();
}
PPB_ImageData_Impl::~PPB_ImageData_Impl() {
}
bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format,
int width, int height,
bool init_to_zero) {
// TODO(brettw) this should be called only on the main thread!
if (!IsImageDataFormatSupported(format))
return false; // Only support this one format for now.
if (width <= 0 || height <= 0)
return false;
if (static_cast<int64>(width) * static_cast<int64>(height) >=
std::numeric_limits<int32>::max() / 4)
return false; // Prevent overflow of signed 32-bit ints.
format_ = format;
width_ = width;
height_ = height;
return backend_->Init(this, format, width, height, init_to_zero);
}
// static
PP_Resource PPB_ImageData_Impl::Create(PP_Instance instance,
PPB_ImageData_Shared::ImageDataType type,
PP_ImageDataFormat format,
const PP_Size& size,
PP_Bool init_to_zero) {
scoped_refptr<PPB_ImageData_Impl>
data(new PPB_ImageData_Impl(instance, type));
if (!data->Init(format, size.width, size.height, !!init_to_zero))
return 0;
return data->GetReference();
}
PPB_ImageData_API* PPB_ImageData_Impl::AsPPB_ImageData_API() {
return this;
}
bool PPB_ImageData_Impl::IsMapped() const {
return backend_->IsMapped();
}
TransportDIB* PPB_ImageData_Impl::GetTransportDIB() const {
return backend_->GetTransportDIB();
}
PP_Bool PPB_ImageData_Impl::Describe(PP_ImageDataDesc* desc) {
desc->format = format_;
desc->size.width = width_;
desc->size.height = height_;
desc->stride = width_ * 4;
return PP_TRUE;
}
void* PPB_ImageData_Impl::Map() {
return backend_->Map();
}
void PPB_ImageData_Impl::Unmap() {
backend_->Unmap();
}
int32_t PPB_ImageData_Impl::GetSharedMemory(int* handle, uint32_t* byte_count) {
return backend_->GetSharedMemory(handle, byte_count);
}
skia::PlatformCanvas* PPB_ImageData_Impl::GetPlatformCanvas() {
return backend_->GetPlatformCanvas();
}
SkCanvas* PPB_ImageData_Impl::GetCanvas() {
return backend_->GetCanvas();
}
void PPB_ImageData_Impl::SetIsCandidateForReuse() {
// Nothing to do since we don't support image data re-use in-process.
}
const SkBitmap* PPB_ImageData_Impl::GetMappedBitmap() const {
return backend_->GetMappedBitmap();
}
// ImageDataPlatformBackend ----------------------------------------------------
ImageDataPlatformBackend::ImageDataPlatformBackend()
: width_(0),
height_(0) {
}
// On POSIX, we have to tell the browser to free the transport DIB.
ImageDataPlatformBackend::~ImageDataPlatformBackend() {
#if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
if (dib_) {
RenderThreadImpl::current()->Send(
new ViewHostMsg_FreeTransportDIB(dib_->id()));
}
#endif
}
bool ImageDataPlatformBackend::Init(PPB_ImageData_Impl* impl,
PP_ImageDataFormat format,
int width, int height,
bool init_to_zero) {
// TODO(brettw) use init_to_zero when we implement caching.
width_ = width;
height_ = height;
uint32 buffer_size = width_ * height_ * 4;
// Allocate the transport DIB and the PlatformCanvas pointing to it.
#if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
// On the Mac, shared memory has to be created in the browser in order to
// work in the sandbox. Do this by sending a message to the browser
// requesting a TransportDIB (see also
// chrome/renderer/webplugin_delegate_proxy.cc, method
// WebPluginDelegateProxy::CreateBitmap() for similar code). The TransportDIB
// is cached in the browser, and is freed (in typical cases) by the
// TransportDIB's destructor.
TransportDIB::Handle dib_handle;
IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(buffer_size,
true,
&dib_handle);
if (!RenderThreadImpl::current()->Send(msg))
return false;
if (!TransportDIB::is_valid_handle(dib_handle))
return false;
TransportDIB* dib = TransportDIB::CreateWithHandle(dib_handle);
#else
static int next_dib_id = 0;
TransportDIB* dib = TransportDIB::Create(buffer_size, next_dib_id++);
if (!dib)
return false;
#endif
dib_.reset(dib);
return true;
}
bool ImageDataPlatformBackend::IsMapped() const {
return !!mapped_canvas_.get();
}
TransportDIB* ImageDataPlatformBackend::GetTransportDIB() const {
return dib_.get();
}
void* ImageDataPlatformBackend::Map() {
if (!mapped_canvas_) {
mapped_canvas_.reset(dib_->GetPlatformCanvas(width_, height_));
if (!mapped_canvas_)
return NULL;
}
const SkBitmap& bitmap =
skia::GetTopDevice(*mapped_canvas_)->accessBitmap(true);
// Our platform bitmaps are set to opaque by default, which we don't want.
const_cast<SkBitmap&>(bitmap).setIsOpaque(false);
bitmap.lockPixels();
return bitmap.getAddr32(0, 0);
}
void ImageDataPlatformBackend::Unmap() {
// This is currently unimplemented, which is OK. The data will just always
// be around once it's mapped. Chrome's TransportDIB isn't currently
// unmappable without freeing it, but this may be something we want to support
// in the future to save some memory.
}
int32_t ImageDataPlatformBackend::GetSharedMemory(int* handle,
uint32_t* byte_count) {
*byte_count = dib_->size();
#if defined(OS_WIN)
*handle = reinterpret_cast<intptr_t>(dib_->handle());
#elif defined(TOOLKIT_GTK)
*handle = static_cast<intptr_t>(dib_->handle());
#else
*handle = static_cast<intptr_t>(dib_->handle().fd);
#endif
return PP_OK;
}
skia::PlatformCanvas* ImageDataPlatformBackend::GetPlatformCanvas() {
return mapped_canvas_.get();
}
SkCanvas* ImageDataPlatformBackend::GetCanvas() {
return mapped_canvas_.get();
}
const SkBitmap* ImageDataPlatformBackend::GetMappedBitmap() const {
if (!mapped_canvas_)
return NULL;
return &skia::GetTopDevice(*mapped_canvas_)->accessBitmap(false);
}
// ImageDataSimpleBackend ------------------------------------------------------
ImageDataSimpleBackend::ImageDataSimpleBackend()
: map_count_(0) {
}
ImageDataSimpleBackend::~ImageDataSimpleBackend() {
}
bool ImageDataSimpleBackend::Init(PPB_ImageData_Impl* impl,
PP_ImageDataFormat format,
int width, int height,
bool init_to_zero) {
skia_bitmap_.setConfig(SkBitmap::kARGB_8888_Config,
impl->width(), impl->height());
PepperPluginDelegateImpl* plugin_delegate =
ResourceHelper::GetPluginDelegate(impl);
if (!plugin_delegate)
return false;
shared_memory_.reset(RenderThread::Get()->HostAllocateSharedMemoryBuffer(
skia_bitmap_.getSize()).release());
return !!shared_memory_.get();
}
bool ImageDataSimpleBackend::IsMapped() const {
return map_count_ > 0;
}
TransportDIB* ImageDataSimpleBackend::GetTransportDIB() const {
return NULL;
}
void* ImageDataSimpleBackend::Map() {
DCHECK(shared_memory_.get());
if (map_count_++ == 0) {
shared_memory_->Map(skia_bitmap_.getSize());
skia_bitmap_.setPixels(shared_memory_->memory());
// Our platform bitmaps are set to opaque by default, which we don't want.
skia_bitmap_.setIsOpaque(false);
skia_canvas_.reset(new SkCanvas(skia_bitmap_));
return skia_bitmap_.getAddr32(0, 0);
}
return shared_memory_->memory();
}
void ImageDataSimpleBackend::Unmap() {
if (--map_count_ == 0)
shared_memory_->Unmap();
}
int32_t ImageDataSimpleBackend::GetSharedMemory(int* handle,
uint32_t* byte_count) {
*byte_count = skia_bitmap_.getSize();
#if defined(OS_POSIX)
*handle = shared_memory_->handle().fd;
#elif defined(OS_WIN)
*handle = reinterpret_cast<int>(shared_memory_->handle());
#else
#error "Platform not supported."
#endif
return PP_OK;
}
skia::PlatformCanvas* ImageDataSimpleBackend::GetPlatformCanvas() {
return NULL;
}
SkCanvas* ImageDataSimpleBackend::GetCanvas() {
if (!IsMapped())
return NULL;
return skia_canvas_.get();
}
const SkBitmap* ImageDataSimpleBackend::GetMappedBitmap() const {
if (!IsMapped())
return NULL;
return &skia_bitmap_;
}
} // namespace content
|