OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2010 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 <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include "ppapi/c/pp_completion_callback.h" | 8 #include "ppapi/c/pp_completion_callback.h" |
9 #include "ppapi/c/pp_errors.h" | 9 #include "ppapi/c/pp_errors.h" |
10 #include "ppapi/c/pp_instance.h" | 10 #include "ppapi/c/pp_instance.h" |
11 #include "ppapi/c/pp_module.h" | 11 #include "ppapi/c/pp_module.h" |
12 #include "ppapi/c/pp_size.h" | 12 #include "ppapi/c/pp_size.h" |
13 #include "ppapi/c/pp_var.h" | 13 #include "ppapi/c/pp_var.h" |
14 #include "ppapi/c/ppb.h" | 14 #include "ppapi/c/ppb.h" |
15 #include "ppapi/c/ppb_core.h" | 15 #include "ppapi/c/ppb_core.h" |
16 #include "ppapi/c/ppb_graphics_2d.h" | 16 #include "ppapi/c/ppb_graphics_2d.h" |
17 #include "ppapi/c/ppb_image_data.h" | 17 #include "ppapi/c/ppb_image_data.h" |
18 #include "ppapi/c/ppb_instance.h" | 18 #include "ppapi/c/ppb_instance.h" |
19 #include "ppapi/c/ppp.h" | 19 #include "ppapi/c/ppp.h" |
20 #include "ppapi/c/ppp_instance.h" | 20 #include "ppapi/c/ppp_instance.h" |
21 | 21 |
22 PP_Module g_module_id; | 22 PP_Module g_module_id; |
23 PPB_GetInterface g_get_browser_interface = NULL; | 23 PPB_GetInterface g_get_browser_interface = NULL; |
24 | 24 |
25 const struct PPB_Core* g_core_interface; | 25 const struct PPB_Core* g_core_interface; |
26 const struct PPB_Graphics2D* g_graphics_2d_interface; | 26 const struct PPB_Graphics2D* g_graphics_2d_interface; |
27 const struct PPB_ImageData* g_image_data_interface; | 27 const struct PPB_ImageData* g_image_data_interface; |
28 const struct PPB_Instance* g_instance_interface; | 28 const struct PPB_Instance* g_instance_interface; |
29 | 29 |
30 // PPP_Instance implementation ------------------------------------------------- | 30 /* PPP_Instance implementation -----------------------------------------------*/ |
31 | 31 |
32 struct InstanceInfo { | 32 struct InstanceInfo { |
33 PP_Instance pp_instance; | 33 PP_Instance pp_instance; |
34 struct PP_Size last_size; | 34 struct PP_Size last_size; |
35 | 35 |
36 struct InstanceInfo* next; | 36 struct InstanceInfo* next; |
37 }; | 37 }; |
38 | 38 |
39 // Linked list of all live instances. | 39 /** Linked list of all live instances. */ |
40 struct InstanceInfo* all_instances = NULL; | 40 struct InstanceInfo* all_instances = NULL; |
41 | 41 |
42 // Returns a refed resource corresponding to the created device context. | 42 /** Returns a refed resource corresponding to the created device context. */ |
43 PP_Resource MakeAndBindDeviceContext(PP_Instance instance, | 43 PP_Resource MakeAndBindDeviceContext(PP_Instance instance, |
44 const struct PP_Size* size) { | 44 const struct PP_Size* size) { |
45 PP_Resource device_context; | 45 PP_Resource device_context; |
46 | 46 |
47 device_context = g_graphics_2d_interface->Create(g_module_id, size, PP_FALSE); | 47 device_context = g_graphics_2d_interface->Create(g_module_id, size, PP_FALSE); |
48 if (!device_context) | 48 if (!device_context) |
49 return 0; | 49 return 0; |
50 | 50 |
51 if (!g_instance_interface->BindGraphics(instance, device_context)) { | 51 if (!g_instance_interface->BindGraphics(instance, device_context)) { |
52 g_core_interface->ReleaseResource(device_context); | 52 g_core_interface->ReleaseResource(device_context); |
53 return 0; | 53 return 0; |
54 } | 54 } |
55 return device_context; | 55 return device_context; |
56 } | 56 } |
57 | 57 |
58 void FlushCompletionCallback(void* user_data, int32_t result) { | 58 void FlushCompletionCallback(void* user_data, int32_t result) { |
59 // Don't need to do anything here. | 59 /* Don't need to do anything here. */ |
60 } | 60 } |
61 | 61 |
62 void Repaint(struct InstanceInfo* instance, const struct PP_Size* size) { | 62 void Repaint(struct InstanceInfo* instance, const struct PP_Size* size) { |
63 PP_Resource image, device_context; | 63 PP_Resource image, device_context; |
64 struct PP_ImageDataDesc image_desc; | 64 struct PP_ImageDataDesc image_desc; |
65 uint32_t* image_data; | 65 uint32_t* image_data; |
66 int num_words, i; | 66 int num_words, i; |
67 | 67 |
68 // Create image data to paint into. | 68 /* Create image data to paint into. */ |
69 image = g_image_data_interface->Create( | 69 image = g_image_data_interface->Create( |
70 g_module_id, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, PP_TRUE); | 70 g_module_id, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, PP_TRUE); |
71 if (!image) | 71 if (!image) |
72 return; | 72 return; |
73 g_image_data_interface->Describe(image, &image_desc); | 73 g_image_data_interface->Describe(image, &image_desc); |
74 | 74 |
75 // Fill the image with blue. | 75 /* Fill the image with blue. */ |
76 image_data = (uint32_t*)g_image_data_interface->Map(image); | 76 image_data = (uint32_t*)g_image_data_interface->Map(image); |
77 if (!image_data) { | 77 if (!image_data) { |
78 g_core_interface->ReleaseResource(image); | 78 g_core_interface->ReleaseResource(image); |
79 return; | 79 return; |
80 } | 80 } |
81 num_words = image_desc.stride * size->height / 4; | 81 num_words = image_desc.stride * size->height / 4; |
82 for (i = 0; i < num_words; i++) | 82 for (i = 0; i < num_words; i++) |
83 image_data[i] = 0xFF0000FF; | 83 image_data[i] = 0xFF0000FF; |
84 | 84 |
85 // Create the device context and paint the image to it. | 85 /* Create the device context and paint the image to it. */ |
86 device_context = MakeAndBindDeviceContext(instance->pp_instance, size); | 86 device_context = MakeAndBindDeviceContext(instance->pp_instance, size); |
87 if (!device_context) { | 87 if (!device_context) { |
88 g_core_interface->ReleaseResource(image); | 88 g_core_interface->ReleaseResource(image); |
89 return; | 89 return; |
90 } | 90 } |
91 | 91 |
92 g_graphics_2d_interface->ReplaceContents(device_context, image); | 92 g_graphics_2d_interface->ReplaceContents(device_context, image); |
93 g_graphics_2d_interface->Flush(device_context, | 93 g_graphics_2d_interface->Flush(device_context, |
94 PP_MakeCompletionCallback(&FlushCompletionCallback, NULL)); | 94 PP_MakeCompletionCallback(&FlushCompletionCallback, NULL)); |
95 | 95 |
96 g_core_interface->ReleaseResource(device_context); | 96 g_core_interface->ReleaseResource(device_context); |
97 g_core_interface->ReleaseResource(image); | 97 g_core_interface->ReleaseResource(image); |
98 } | 98 } |
99 | 99 |
100 // Returns the info for the given instance, or NULL if it's not found. | 100 /** Returns the info for the given instance, or NULL if it's not found. */ |
101 struct InstanceInfo* FindInstance(PP_Instance instance) { | 101 struct InstanceInfo* FindInstance(PP_Instance instance) { |
102 struct InstanceInfo* cur = all_instances; | 102 struct InstanceInfo* cur = all_instances; |
103 while (cur) { | 103 while (cur) { |
104 if (cur->pp_instance == instance) | 104 if (cur->pp_instance == instance) |
105 return cur; | 105 return cur; |
106 } | 106 } |
107 return NULL; | 107 return NULL; |
108 } | 108 } |
109 | 109 |
110 PP_Bool Instance_DidCreate(PP_Instance instance, | 110 PP_Bool Instance_DidCreate(PP_Instance instance, |
111 uint32_t argc, | 111 uint32_t argc, |
112 const char* argn[], | 112 const char* argn[], |
113 const char* argv[]) { | 113 const char* argv[]) { |
114 struct InstanceInfo* info = | 114 struct InstanceInfo* info = |
115 (struct InstanceInfo*)malloc(sizeof(struct InstanceInfo)); | 115 (struct InstanceInfo*)malloc(sizeof(struct InstanceInfo)); |
116 info->pp_instance = instance; | 116 info->pp_instance = instance; |
117 info->last_size.width = 0; | 117 info->last_size.width = 0; |
118 info->last_size.height = 0; | 118 info->last_size.height = 0; |
119 | 119 |
120 // Insert into linked list of live instances. | 120 /* Insert into linked list of live instances. */ |
121 info->next = all_instances; | 121 info->next = all_instances; |
122 all_instances = info; | 122 all_instances = info; |
123 return PP_TRUE; | 123 return PP_TRUE; |
124 } | 124 } |
125 | 125 |
126 void Instance_DidDestroy(PP_Instance instance) { | 126 void Instance_DidDestroy(PP_Instance instance) { |
127 // Find the matching item in the linked list, delete it, and patch the links. | 127 /* Find the matching item in the linked list, delete it, and patch the |
| 128 * links. |
| 129 */ |
128 struct InstanceInfo** prev_ptr = &all_instances; | 130 struct InstanceInfo** prev_ptr = &all_instances; |
129 struct InstanceInfo* cur = all_instances; | 131 struct InstanceInfo* cur = all_instances; |
130 while (cur) { | 132 while (cur) { |
131 if (instance == cur->pp_instance) { | 133 if (instance == cur->pp_instance) { |
132 *prev_ptr = cur->next; | 134 *prev_ptr = cur->next; |
133 free(cur); | 135 free(cur); |
134 return; | 136 return; |
135 } | 137 } |
136 prev_ptr = &cur->next; | 138 prev_ptr = &cur->next; |
137 } | 139 } |
138 } | 140 } |
139 | 141 |
140 void Instance_DidChangeView(PP_Instance pp_instance, | 142 void Instance_DidChangeView(PP_Instance pp_instance, |
141 const struct PP_Rect* position, | 143 const struct PP_Rect* position, |
142 const struct PP_Rect* clip) { | 144 const struct PP_Rect* clip) { |
143 struct InstanceInfo* info = FindInstance(pp_instance); | 145 struct InstanceInfo* info = FindInstance(pp_instance); |
144 if (!info) | 146 if (!info) |
145 return; | 147 return; |
146 | 148 |
147 if (info->last_size.width != position->size.width || | 149 if (info->last_size.width != position->size.width || |
148 info->last_size.height != position->size.height) { | 150 info->last_size.height != position->size.height) { |
149 // Got a resize, repaint the plugin. | 151 /* Got a resize, repaint the plugin. */ |
150 Repaint(info, &position->size); | 152 Repaint(info, &position->size); |
151 info->last_size.width = position->size.width; | 153 info->last_size.width = position->size.width; |
152 info->last_size.height = position->size.height; | 154 info->last_size.height = position->size.height; |
153 } | 155 } |
154 } | 156 } |
155 | 157 |
156 void Instance_DidChangeFocus(PP_Instance pp_instance, PP_Bool has_focus) { | 158 void Instance_DidChangeFocus(PP_Instance pp_instance, PP_Bool has_focus) { |
157 } | 159 } |
158 | 160 |
159 PP_Bool Instance_HandleInputEvent(PP_Instance pp_instance, | 161 PP_Bool Instance_HandleInputEvent(PP_Instance pp_instance, |
160 const struct PP_InputEvent* event) { | 162 const struct PP_InputEvent* event) { |
161 // We don't handle any events. | 163 /* We don't handle any events. */ |
162 return PP_FALSE; | 164 return PP_FALSE; |
163 } | 165 } |
164 | 166 |
165 PP_Bool Instance_HandleDocumentLoad(PP_Instance pp_instance, | 167 PP_Bool Instance_HandleDocumentLoad(PP_Instance pp_instance, |
166 PP_Resource pp_url_loader) { | 168 PP_Resource pp_url_loader) { |
167 return PP_FALSE; | 169 return PP_FALSE; |
168 } | 170 } |
169 | 171 |
170 struct PP_Var Instance_GetInstanceObject(PP_Instance pp_instance) { | 172 struct PP_Var Instance_GetInstanceObject(PP_Instance pp_instance) { |
171 return PP_MakeNull(); | 173 return PP_MakeNull(); |
172 } | 174 } |
173 | 175 |
174 static struct PPP_Instance instance_interface = { | 176 static struct PPP_Instance instance_interface = { |
175 &Instance_DidCreate, | 177 &Instance_DidCreate, |
176 &Instance_DidDestroy, | 178 &Instance_DidDestroy, |
177 &Instance_DidChangeView, | 179 &Instance_DidChangeView, |
178 &Instance_DidChangeFocus, | 180 &Instance_DidChangeFocus, |
179 &Instance_HandleInputEvent, | 181 &Instance_HandleInputEvent, |
180 &Instance_HandleDocumentLoad, | 182 &Instance_HandleDocumentLoad, |
181 &Instance_GetInstanceObject, | 183 &Instance_GetInstanceObject, |
182 }; | 184 }; |
183 | 185 |
184 | 186 |
185 // Global entrypoints ---------------------------------------------------------- | 187 /* Global entrypoints --------------------------------------------------------*/ |
186 | 188 |
187 PP_EXPORT int32_t PPP_InitializeModule(PP_Module module, | 189 PP_EXPORT int32_t PPP_InitializeModule(PP_Module module, |
188 PPB_GetInterface get_browser_interface) { | 190 PPB_GetInterface get_browser_interface) { |
189 // Save the global module information for later. | 191 /* Save the global module information for later. */ |
190 g_module_id = module; | 192 g_module_id = module; |
191 g_get_browser_interface = get_browser_interface; | 193 g_get_browser_interface = get_browser_interface; |
192 | 194 |
193 g_core_interface = (const struct PPB_Core*) | 195 g_core_interface = (const struct PPB_Core*) |
194 get_browser_interface(PPB_CORE_INTERFACE); | 196 get_browser_interface(PPB_CORE_INTERFACE); |
195 g_instance_interface = (const struct PPB_Instance*) | 197 g_instance_interface = (const struct PPB_Instance*) |
196 get_browser_interface(PPB_INSTANCE_INTERFACE); | 198 get_browser_interface(PPB_INSTANCE_INTERFACE); |
197 g_image_data_interface = (const struct PPB_ImageData*) | 199 g_image_data_interface = (const struct PPB_ImageData*) |
198 get_browser_interface(PPB_IMAGEDATA_INTERFACE); | 200 get_browser_interface(PPB_IMAGEDATA_INTERFACE); |
199 g_graphics_2d_interface = (const struct PPB_Graphics2D*) | 201 g_graphics_2d_interface = (const struct PPB_Graphics2D*) |
200 get_browser_interface(PPB_GRAPHICS_2D_INTERFACE); | 202 get_browser_interface(PPB_GRAPHICS_2D_INTERFACE); |
201 if (!g_core_interface || !g_instance_interface || !g_image_data_interface || | 203 if (!g_core_interface || !g_instance_interface || !g_image_data_interface || |
202 !g_graphics_2d_interface) | 204 !g_graphics_2d_interface) |
203 return -1; | 205 return -1; |
204 | 206 |
205 return PP_OK; | 207 return PP_OK; |
206 } | 208 } |
207 | 209 |
208 PP_EXPORT void PPP_ShutdownModule() { | 210 PP_EXPORT void PPP_ShutdownModule() { |
209 } | 211 } |
210 | 212 |
211 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { | 213 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { |
212 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) | 214 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) |
213 return &instance_interface; | 215 return &instance_interface; |
214 return NULL; | 216 return NULL; |
215 } | 217 } |
| 218 |
OLD | NEW |