Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(599)

Side by Side Diff: ppapi/examples/2d/graphics_2d_example.c

Issue 1065733003: PPAPI Examples: Fix painting in Graphics 2D example. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add periods Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 /* Copyright (c) 2012 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"
(...skipping 15 matching lines...) Expand all
26 const PPB_Graphics2D* g_graphics_2d_interface; 26 const PPB_Graphics2D* g_graphics_2d_interface;
27 const PPB_ImageData* g_image_data_interface; 27 const PPB_ImageData* g_image_data_interface;
28 const PPB_Instance* g_instance_interface; 28 const PPB_Instance* g_instance_interface;
29 const PPB_View* g_view_interface; 29 const PPB_View* g_view_interface;
30 30
31 /* PPP_Instance implementation -----------------------------------------------*/ 31 /* PPP_Instance implementation -----------------------------------------------*/
32 32
33 struct InstanceInfo { 33 struct InstanceInfo {
34 PP_Instance pp_instance; 34 PP_Instance pp_instance;
35 struct PP_Size last_size; 35 struct PP_Size last_size;
36 PP_Resource graphics;
36 37
37 struct InstanceInfo* next; 38 struct InstanceInfo* next;
38 }; 39 };
39 40
40 /** Linked list of all live instances. */ 41 /** Linked list of all live instances. */
41 struct InstanceInfo* all_instances = NULL; 42 struct InstanceInfo* all_instances = NULL;
42 43
43 /** Returns a refed resource corresponding to the created graphics 2d. */ 44 /** Returns a refed resource corresponding to the created graphics 2d. */
44 PP_Resource MakeAndBindGraphics2D(PP_Instance instance, 45 PP_Resource MakeAndBindGraphics2D(PP_Instance instance,
45 const struct PP_Size* size) { 46 const struct PP_Size* size) {
46 PP_Resource graphics; 47 PP_Resource graphics;
47 48
48 graphics = g_graphics_2d_interface->Create(instance, size, PP_FALSE); 49 graphics = g_graphics_2d_interface->Create(instance, size, PP_FALSE);
49 if (!graphics) 50 if (!graphics)
50 return 0; 51 return 0;
51 52
52 if (!g_instance_interface->BindGraphics(instance, graphics)) { 53 if (!g_instance_interface->BindGraphics(instance, graphics)) {
53 g_core_interface->ReleaseResource(graphics); 54 g_core_interface->ReleaseResource(graphics);
54 return 0; 55 return 0;
55 } 56 }
56 return graphics; 57 return graphics;
57 } 58 }
58 59
59 void FlushCompletionCallback(void* user_data, int32_t result) { 60 void FlushCompletionCallback(void* user_data, int32_t result) {
60 /* Don't need to do anything here. */ 61 /* Don't need to do anything here. */
61 } 62 }
62 63
63 void Repaint(struct InstanceInfo* instance, const struct PP_Size* size) { 64 void Repaint(struct InstanceInfo* instance, const struct PP_Size* size) {
64 PP_Resource image, graphics; 65 PP_Resource image;
65 struct PP_ImageDataDesc image_desc; 66 struct PP_ImageDataDesc image_desc;
66 uint32_t* image_data; 67 uint32_t* image_data;
67 int num_words, i; 68 int num_words, i;
68 69
70 /* Ensure the graphics 2d is ready. */
71 if (!instance->graphics) {
72 instance->graphics = MakeAndBindGraphics2D(instance->pp_instance, size);
73 if (!instance->graphics)
74 return;
75 }
76
69 /* Create image data to paint into. */ 77 /* Create image data to paint into. */
70 image = g_image_data_interface->Create( 78 image = g_image_data_interface->Create(
71 instance->pp_instance, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, PP_TRUE); 79 instance->pp_instance, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, PP_TRUE);
72 if (!image) 80 if (!image)
73 return; 81 return;
74 g_image_data_interface->Describe(image, &image_desc); 82 g_image_data_interface->Describe(image, &image_desc);
75 83
76 /* Fill the image with blue. */ 84 /* Fill the image with blue. */
77 image_data = (uint32_t*)g_image_data_interface->Map(image); 85 image_data = (uint32_t*)g_image_data_interface->Map(image);
78 if (!image_data) { 86 if (!image_data) {
79 g_core_interface->ReleaseResource(image); 87 g_core_interface->ReleaseResource(image);
80 return; 88 return;
81 } 89 }
82 num_words = image_desc.stride * size->height / 4; 90 num_words = image_desc.stride * size->height / 4;
83 for (i = 0; i < num_words; i++) 91 for (i = 0; i < num_words; i++)
84 image_data[i] = 0xFF0000FF; 92 image_data[i] = 0xFF0000FF;
85 93
86 /* Create the graphics 2d and paint the image to it. */ 94 /* Paint image to graphics 2d. */
87 graphics = MakeAndBindGraphics2D(instance->pp_instance, size); 95 g_graphics_2d_interface->ReplaceContents(instance->graphics, image);
88 if (!graphics) { 96 g_graphics_2d_interface->Flush(instance->graphics,
89 g_core_interface->ReleaseResource(image);
90 return;
91 }
92
93 g_graphics_2d_interface->ReplaceContents(graphics, image);
94 g_graphics_2d_interface->Flush(graphics,
95 PP_MakeCompletionCallback(&FlushCompletionCallback, NULL)); 97 PP_MakeCompletionCallback(&FlushCompletionCallback, NULL));
dmichael (off chromium) 2015/04/07 15:33:13 Yeah, in the old version there's no guarantee the
96 98
97 g_core_interface->ReleaseResource(graphics);
98 g_core_interface->ReleaseResource(image); 99 g_core_interface->ReleaseResource(image);
99 } 100 }
100 101
101 /** Returns the info for the given instance, or NULL if it's not found. */ 102 /** Returns the info for the given instance, or NULL if it's not found. */
102 struct InstanceInfo* FindInstance(PP_Instance instance) { 103 struct InstanceInfo* FindInstance(PP_Instance instance) {
103 struct InstanceInfo* cur = all_instances; 104 struct InstanceInfo* cur = all_instances;
104 while (cur) { 105 while (cur) {
105 if (cur->pp_instance == instance) 106 if (cur->pp_instance == instance)
106 return cur; 107 return cur;
107 cur = cur->next; 108 cur = cur->next;
108 } 109 }
109 return NULL; 110 return NULL;
110 } 111 }
111 112
112 PP_Bool Instance_DidCreate(PP_Instance instance, 113 PP_Bool Instance_DidCreate(PP_Instance instance,
113 uint32_t argc, 114 uint32_t argc,
114 const char* argn[], 115 const char* argn[],
115 const char* argv[]) { 116 const char* argv[]) {
116 struct InstanceInfo* info = 117 struct InstanceInfo* info =
117 (struct InstanceInfo*)malloc(sizeof(struct InstanceInfo)); 118 (struct InstanceInfo*)malloc(sizeof(struct InstanceInfo));
118 info->pp_instance = instance; 119 info->pp_instance = instance;
119 info->last_size.width = 0; 120 info->last_size.width = 0;
120 info->last_size.height = 0; 121 info->last_size.height = 0;
122 info->graphics = 0;
121 123
122 /* Insert into linked list of live instances. */ 124 /* Insert into linked list of live instances. */
123 info->next = all_instances; 125 info->next = all_instances;
124 all_instances = info; 126 all_instances = info;
125 return PP_TRUE; 127 return PP_TRUE;
126 } 128 }
127 129
128 void Instance_DidDestroy(PP_Instance instance) { 130 void Instance_DidDestroy(PP_Instance instance) {
129 /* Find the matching item in the linked list, delete it, and patch the 131 /* Find the matching item in the linked list, delete it, and patch the
130 * links. 132 * links.
131 */ 133 */
132 struct InstanceInfo** prev_ptr = &all_instances; 134 struct InstanceInfo** prev_ptr = &all_instances;
133 struct InstanceInfo* cur = all_instances; 135 struct InstanceInfo* cur = all_instances;
134 while (cur) { 136 while (cur) {
135 if (instance == cur->pp_instance) { 137 if (instance == cur->pp_instance) {
136 *prev_ptr = cur->next; 138 *prev_ptr = cur->next;
139 g_core_interface->ReleaseResource(cur->graphics);
137 free(cur); 140 free(cur);
138 return; 141 return;
139 } 142 }
140 prev_ptr = &cur->next; 143 prev_ptr = &cur->next;
141 cur = cur->next; 144 cur = cur->next;
142 } 145 }
143 } 146 }
144 147
145 void Instance_DidChangeView(PP_Instance pp_instance, 148 void Instance_DidChangeView(PP_Instance pp_instance,
146 PP_Resource view) { 149 PP_Resource view) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 } 205 }
203 206
204 PP_EXPORT void PPP_ShutdownModule() { 207 PP_EXPORT void PPP_ShutdownModule() {
205 } 208 }
206 209
207 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { 210 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) {
208 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) 211 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0)
209 return &instance_interface; 212 return &instance_interface;
210 return NULL; 213 return NULL;
211 } 214 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698