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
|
// 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_device_context_2d.h"
#include "base/logging.h"
#include "gfx/point.h"
#include "gfx/rect.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/ppapi/c/pp_module.h"
#include "third_party/ppapi/c/pp_rect.h"
#include "third_party/ppapi/c/pp_resource.h"
#include "third_party/ppapi/c/ppb_device_context_2d.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "webkit/glue/plugins/pepper_image_data.h"
#include "webkit/glue/plugins/pepper_plugin_module.h"
#include "webkit/glue/plugins/pepper_resource_tracker.h"
#if defined(OS_MACOSX)
#include "base/mac_util.h"
#include "base/scoped_cftyperef.h"
#endif
namespace pepper {
namespace {
PP_Resource Create(PP_Module module_id, int32_t width, int32_t height) {
PluginModule* module = PluginModule::FromPPModule(module_id);
if (!module)
return NullPPResource();
scoped_refptr<DeviceContext2D> context(new DeviceContext2D(module));
if (!context->Init(width, height))
return NullPPResource();
context->AddRef(); // AddRef for the caller.
return context->GetResource();
}
void PaintImageData(PP_Resource device_context,
PP_Resource image,
int32_t x, int32_t y,
const PP_Rect* dirty,
uint32_t dirty_rect_count,
PPB_DeviceContext2D_PaintCallback callback,
void* callback_data) {
scoped_refptr<Resource> device_resource =
ResourceTracker::Get()->GetResource(device_context);
if (!device_resource.get())
return;
DeviceContext2D* context = device_resource->AsDeviceContext2D();
if (!context)
return;
context->PaintImageData(image, x, y, dirty, dirty_rect_count,
callback, callback_data);
}
const PPB_DeviceContext2D ppb_devicecontext2d = {
&Create,
&PaintImageData,
};
} // namespace
DeviceContext2D::DeviceContext2D(PluginModule* module) : Resource(module) {
}
DeviceContext2D::~DeviceContext2D() {
}
// static
const PPB_DeviceContext2D* DeviceContext2D::GetInterface() {
return &ppb_devicecontext2d;
}
bool DeviceContext2D::Init(int width, int height) {
image_data_.reset(new ImageData(module()));
if (!image_data_->Init(PP_IMAGEDATAFORMAT_BGRA_PREMUL, width, height) ||
!image_data_->Map()) {
image_data_.reset();
return false;
}
return true;
}
void DeviceContext2D::PaintImageData(PP_Resource image,
int32_t x, int32_t y,
const PP_Rect* dirty,
uint32_t dirty_rect_count,
PPB_DeviceContext2D_PaintCallback callback,
void* callback_data) {
scoped_refptr<Resource> image_resource =
ResourceTracker::Get()->GetResource(image);
if (!image_resource.get())
return;
ImageData* new_image_data = image_resource->AsImageData();
if (!new_image_data)
return;
const SkBitmap& new_image_bitmap = new_image_data->GetMappedBitmap();
// TODO(brettw) handle multiple dirty rects.
DCHECK(dirty_rect_count == 1);
// Draw the bitmap to the backing store.
SkIRect src_rect;
if (dirty->left == 0 && dirty->top == 0 &&
dirty->right == 0 && dirty->bottom == 0) {
// Default to the entire bitmap.
src_rect.fLeft = 0;
src_rect.fTop = 0;
src_rect.fRight = new_image_bitmap.width();
src_rect.fBottom = new_image_bitmap.height();
} else {
src_rect.fLeft = dirty->left;
src_rect.fTop = dirty->top;
src_rect.fRight = dirty->right;
src_rect.fBottom = dirty->bottom;
}
SkRect dest_rect = { SkIntToScalar(src_rect.fLeft),
SkIntToScalar(src_rect.fTop),
SkIntToScalar(src_rect.fRight),
SkIntToScalar(src_rect.fBottom) };
// We're guaranteed to have a mapped canvas since we mapped it in Init().
skia::PlatformCanvas* backing_canvas = image_data_->mapped_canvas();
// We want to replace the contents of the bitmap rather than blend.
SkPaint paint;
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
backing_canvas->drawBitmapRect(new_image_bitmap,
&src_rect, dest_rect, &paint);
// TODO(brettw) implement invalidate and callbacks!
// Cause the updated part of the screen to be repainted. This will happen
// asynchronously.
/*
gfx::Rect dest_gfx_rect(dirty->left, dirty->top,
dirty->right - dirty->left,
dirty->bottom - dirty->top);
plugin_delegate_->instance()->webplugin()->InvalidateRect(dest_gfx_rect);
// Save the callback to execute later. See |unpainted_flush_callbacks_| in
// the header file.
if (callback) {
unpainted_flush_callbacks_.push_back(
FlushCallbackData(callback, id, context, user_data));
}
*/
}
void DeviceContext2D::Paint(WebKit::WebCanvas* canvas,
const gfx::Rect& plugin_rect,
const gfx::Rect& paint_rect) {
// We're guaranteed to have a mapped canvas since we mapped it in Init().
const SkBitmap& backing_bitmap = image_data_->GetMappedBitmap();
#if defined(OS_MACOSX)
SkAutoLockPixels lock(backing_bitmap);
scoped_cftyperef<CGDataProviderRef> data_provider(
CGDataProviderCreateWithData(
NULL, backing_bitmap.getAddr32(0, 0),
backing_bitmap.rowBytes() * backing_bitmap.height(), NULL));
scoped_cftyperef<CGImageRef> image(
CGImageCreate(
backing_bitmap.width(), backing_bitmap.height(),
8, 32, backing_bitmap.rowBytes(),
mac_util::GetSystemColorSpace(),
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host,
data_provider, NULL, false, kCGRenderingIntentDefault));
// Flip the transform
CGContextSaveGState(canvas);
float window_height = static_cast<float>(CGBitmapContextGetHeight(canvas));
CGContextTranslateCTM(canvas, 0, window_height);
CGContextScaleCTM(canvas, 1.0, -1.0);
CGRect bounds;
bounds.origin.x = plugin_rect.origin().x();
bounds.origin.y = window_height - plugin_rect.origin().y() -
backing_bitmap.height();
bounds.size.width = backing_bitmap.width();
bounds.size.height = backing_bitmap.height();
CGContextDrawImage(canvas, bounds, image);
CGContextRestoreGState(canvas);
#else
gfx::Point origin(plugin_rect.origin().x(), plugin_rect.origin().y());
canvas->drawBitmap(backing_bitmap,
SkIntToScalar(plugin_rect.origin().x()),
SkIntToScalar(plugin_rect.origin().y()));
#endif
}
} // namespace pepper
|