OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/plugins/ppapi/ppb_flash_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_flash_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
12 #include "ppapi/c/dev/ppb_font_dev.h" | |
13 #include "ppapi/c/private/ppb_flash.h" | 12 #include "ppapi/c/private/ppb_flash.h" |
14 #include "ppapi/shared_impl/time_conversion.h" | 13 #include "ppapi/shared_impl/time_conversion.h" |
15 #include "ppapi/shared_impl/var.h" | 14 #include "ppapi/shared_impl/var.h" |
16 #include "ppapi/thunk/enter.h" | 15 #include "ppapi/thunk/enter.h" |
17 #include "ppapi/thunk/ppb_image_data_api.h" | |
18 #include "skia/ext/platform_canvas.h" | |
19 #include "third_party/skia/include/core/SkCanvas.h" | |
20 #include "third_party/skia/include/core/SkMatrix.h" | |
21 #include "third_party/skia/include/core/SkPaint.h" | |
22 #include "third_party/skia/include/core/SkPoint.h" | |
23 #include "third_party/skia/include/core/SkTemplates.h" | |
24 #include "third_party/skia/include/core/SkTypeface.h" | |
25 #include "webkit/plugins/ppapi/common.h" | 16 #include "webkit/plugins/ppapi/common.h" |
26 #include "webkit/plugins/ppapi/host_globals.h" | 17 #include "webkit/plugins/ppapi/host_globals.h" |
27 #include "webkit/plugins/ppapi/plugin_delegate.h" | 18 #include "webkit/plugins/ppapi/plugin_delegate.h" |
28 #include "webkit/plugins/ppapi/plugin_module.h" | 19 #include "webkit/plugins/ppapi/plugin_module.h" |
29 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
30 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" | 21 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" |
31 #include "webkit/plugins/ppapi/resource_helper.h" | 22 #include "webkit/plugins/ppapi/resource_helper.h" |
32 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" | |
33 | 23 |
34 using ppapi::PPTimeToTime; | 24 using ppapi::PPTimeToTime; |
35 using ppapi::StringVar; | 25 using ppapi::StringVar; |
36 using ppapi::thunk::EnterResource; | 26 using ppapi::thunk::EnterResource; |
37 using ppapi::thunk::PPB_ImageData_API; | |
38 using ppapi::thunk::PPB_URLRequestInfo_API; | 27 using ppapi::thunk::PPB_URLRequestInfo_API; |
39 | 28 |
40 namespace webkit { | 29 namespace webkit { |
41 namespace ppapi { | 30 namespace ppapi { |
42 | 31 |
43 namespace { | 32 namespace { |
44 | 33 |
45 void SetInstanceAlwaysOnTop(PP_Instance pp_instance, PP_Bool on_top) { | 34 void SetInstanceAlwaysOnTop(PP_Instance pp_instance, PP_Bool on_top) { |
46 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); | 35 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); |
47 if (!instance) | 36 if (!instance) |
48 return; | 37 return; |
49 instance->set_always_on_top(PPBoolToBool(on_top)); | 38 instance->set_always_on_top(PPBoolToBool(on_top)); |
50 } | 39 } |
51 | 40 |
52 | |
53 PP_Bool DrawGlyphs(PP_Instance, | |
54 PP_Resource pp_image_data, | |
55 const PP_FontDescription_Dev* font_desc, | |
56 uint32_t color, | |
57 PP_Point position, | |
58 PP_Rect clip, | |
59 const float transformation[3][3], | |
60 uint32_t glyph_count, | |
61 const uint16_t glyph_indices[], | |
62 const PP_Point glyph_advances[]) { | |
63 EnterResource<PPB_ImageData_API> enter(pp_image_data, true); | |
64 if (enter.failed()) | |
65 return PP_FALSE; | |
66 PPB_ImageData_Impl* image_resource = | |
67 static_cast<PPB_ImageData_Impl*>(enter.object()); | |
68 | |
69 ImageDataAutoMapper mapper(image_resource); | |
70 if (!mapper.is_valid()) | |
71 return PP_FALSE; | |
72 | |
73 // Set up the typeface. | |
74 StringVar* face_name = StringVar::FromPPVar(font_desc->face); | |
75 if (!face_name) | |
76 return PP_FALSE; | |
77 int style = SkTypeface::kNormal; | |
78 if (font_desc->weight >= PP_FONTWEIGHT_BOLD) | |
79 style |= SkTypeface::kBold; | |
80 if (font_desc->italic) | |
81 style |= SkTypeface::kItalic; | |
82 SkTypeface* typeface = | |
83 SkTypeface::CreateFromName(face_name->value().c_str(), | |
84 static_cast<SkTypeface::Style>(style)); | |
85 if (!typeface) | |
86 return PP_FALSE; | |
87 | |
88 // Set up the canvas. | |
89 SkCanvas* canvas = image_resource->mapped_canvas(); | |
90 canvas->save(); | |
91 | |
92 // Clip is applied in pixels before the transform. | |
93 SkRect clip_rect = { clip.point.x, clip.point.y, | |
94 clip.point.x + clip.size.width, | |
95 clip.point.y + clip.size.height }; | |
96 canvas->clipRect(clip_rect); | |
97 | |
98 // -- Do not return early below this. The canvas needs restoring and the | |
99 // typeface will leak if it's not assigned to the paint (it's refcounted and | |
100 // the refcount is currently 0). | |
101 | |
102 // Convert & set the matrix. | |
103 SkMatrix matrix; | |
104 matrix.set(SkMatrix::kMScaleX, SkFloatToScalar(transformation[0][0])); | |
105 matrix.set(SkMatrix::kMSkewX, SkFloatToScalar(transformation[0][1])); | |
106 matrix.set(SkMatrix::kMTransX, SkFloatToScalar(transformation[0][2])); | |
107 matrix.set(SkMatrix::kMSkewY, SkFloatToScalar(transformation[1][0])); | |
108 matrix.set(SkMatrix::kMScaleY, SkFloatToScalar(transformation[1][1])); | |
109 matrix.set(SkMatrix::kMTransY, SkFloatToScalar(transformation[1][2])); | |
110 matrix.set(SkMatrix::kMPersp0, SkFloatToScalar(transformation[2][0])); | |
111 matrix.set(SkMatrix::kMPersp1, SkFloatToScalar(transformation[2][1])); | |
112 matrix.set(SkMatrix::kMPersp2, SkFloatToScalar(transformation[2][2])); | |
113 canvas->concat(matrix); | |
114 | |
115 SkPaint paint; | |
116 paint.setColor(color); | |
117 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
118 paint.setAntiAlias(true); | |
119 paint.setHinting(SkPaint::kFull_Hinting); | |
120 paint.setTextSize(SkIntToScalar(font_desc->size)); | |
121 paint.setTypeface(typeface); // Takes a ref and manages lifetime. | |
122 paint.setSubpixelText(true); | |
123 paint.setLCDRenderText(true); | |
124 | |
125 SkScalar x = SkIntToScalar(position.x); | |
126 SkScalar y = SkIntToScalar(position.y); | |
127 | |
128 // Build up the skia advances. | |
129 SkAutoSTMalloc<32, SkPoint> storage(glyph_count); | |
130 SkPoint* sk_positions = storage.get(); | |
131 for (uint32_t i = 0; i < glyph_count; i++) { | |
132 sk_positions[i].set(x, y); | |
133 x += SkFloatToScalar(glyph_advances[i].x); | |
134 y += SkFloatToScalar(glyph_advances[i].y); | |
135 } | |
136 | |
137 canvas->drawPosText(glyph_indices, glyph_count * 2, sk_positions, paint); | |
138 | |
139 canvas->restore(); | |
140 return PP_TRUE; | |
141 } | |
142 | |
143 PP_Var GetProxyForURL(PP_Instance pp_instance, const char* url) { | 41 PP_Var GetProxyForURL(PP_Instance pp_instance, const char* url) { |
144 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); | 42 PluginInstance* instance = HostGlobals::Get()->GetInstance(pp_instance); |
145 if (!instance) | 43 if (!instance) |
146 return PP_MakeUndefined(); | 44 return PP_MakeUndefined(); |
147 | 45 |
148 GURL gurl(url); | 46 GURL gurl(url); |
149 if (!gurl.is_valid()) | 47 if (!gurl.is_valid()) |
150 return PP_MakeUndefined(); | 48 return PP_MakeUndefined(); |
151 | 49 |
152 std::string proxy_host = instance->delegate()->ResolveProxy(gurl); | 50 std::string proxy_host = instance->delegate()->ResolveProxy(gurl); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 PluginInstance* instance = module->GetSomeInstance(); | 108 PluginInstance* instance = module->GetSomeInstance(); |
211 if (!instance) | 109 if (!instance) |
212 return PP_MakeUndefined(); | 110 return PP_MakeUndefined(); |
213 | 111 |
214 std::string args = instance->delegate()->GetFlashCommandLineArgs(); | 112 std::string args = instance->delegate()->GetFlashCommandLineArgs(); |
215 return StringVar::StringToPPVar(args); | 113 return StringVar::StringToPPVar(args); |
216 } | 114 } |
217 | 115 |
218 const PPB_Flash ppb_flash = { | 116 const PPB_Flash ppb_flash = { |
219 &SetInstanceAlwaysOnTop, | 117 &SetInstanceAlwaysOnTop, |
220 &DrawGlyphs, | 118 &PPB_Flash_Impl::DrawGlyphs, |
221 &GetProxyForURL, | 119 &GetProxyForURL, |
222 &Navigate, | 120 &Navigate, |
223 &RunMessageLoop, | 121 &RunMessageLoop, |
224 &QuitMessageLoop, | 122 &QuitMessageLoop, |
225 &GetLocalTimeZoneOffset, | 123 &GetLocalTimeZoneOffset, |
226 &GetCommandLineArgs | 124 &GetCommandLineArgs |
227 }; | 125 }; |
228 | 126 |
229 } // namespace | 127 } // namespace |
230 | 128 |
231 // static | 129 // static |
232 const PPB_Flash* PPB_Flash_Impl::GetInterface() { | 130 const PPB_Flash* PPB_Flash_Impl::GetInterface() { |
233 return &ppb_flash; | 131 return &ppb_flash; |
234 } | 132 } |
235 | 133 |
236 } // namespace ppapi | 134 } // namespace ppapi |
237 } // namespace webkit | 135 } // namespace webkit |
OLD | NEW |