Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / native_client_sdk / src / libraries / ppapi_simple / ps_context_2d.c
blob5760f021bfc52ad1b7480c19591e3527859361aa
1 /* Copyright 2013 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file. */
5 #include <stdlib.h>
6 #include <string.h>
8 #include "ppapi/c/pp_rect.h"
9 #include "ppapi/c/pp_resource.h"
10 #include "ppapi/c/pp_size.h"
11 #include "ppapi/c/ppb_core.h"
12 #include "ppapi/c/ppb_graphics_2d.h"
13 #include "ppapi/c/ppb_image_data.h"
14 #include "ppapi/c/ppb_instance.h"
15 #include "ppapi/c/ppb_view.h"
17 #include "ppapi_simple/ps.h"
18 #include "ppapi_simple/ps_context_2d.h"
19 #include "ppapi_simple/ps_event.h"
20 #include "ppapi_simple/ps_instance.h"
21 #include "ppapi_simple/ps_interface.h"
23 PSContext2D_t* PSContext2DAllocate(PP_ImageDataFormat format) {
24 PSContext2D_t* ctx = (PSContext2D_t*)malloc(sizeof(PSContext2D_t));
25 memset(ctx, 0, sizeof(PSContext2D_t));
27 ctx->format = format;
28 return ctx;
31 void PSContext2DFree(PSContext2D_t* ctx) {
32 if (ctx->graphic_2d) {
33 PSInterfaceCore()->ReleaseResource(ctx->graphic_2d);
34 ctx->graphic_2d = 0;
36 if (ctx->image) {
37 PSInterfaceCore()->ReleaseResource(ctx->image);
38 ctx->image = 0;
40 free(ctx);
43 PP_ImageDataFormat PSContext2DGetNativeImageDataFormat() {
44 return PSInterfaceImageData()->GetNativeImageDataFormat();
47 // Update the 2D context if the message is appropriate, returning non-zero
48 // if the event was consumed.
49 int PSContext2DHandleEvent(PSContext2D_t* ctx, PSEvent* event) {
50 switch (event->type) {
51 case PSE_INSTANCE_DIDCHANGEVIEW: {
52 struct PP_Rect rect;
54 PSInterfaceView()->GetRect(event->as_resource, &rect);
55 PSInterfaceCore()->ReleaseResource(ctx->graphic_2d);
56 ctx->bound = 0;
57 ctx->width = rect.size.width;
58 ctx->height = rect.size.height;
60 // Create an opaque graphic context of the specified size.
61 ctx->graphic_2d = PSInterfaceGraphics2D()->Create(PSGetInstanceId(),
62 &rect.size, PP_TRUE);
64 // Bind the context to so that draws will be visible.
65 if (ctx->graphic_2d) {
66 ctx->bound = PSInterfaceInstance()->BindGraphics(PSGetInstanceId(),
67 ctx->graphic_2d);
70 // Typically this resource would not be allocated yet, but just in case
71 // throw it away, to force a new allocation when GetBuffer is called.
72 if (ctx->image) {
73 PSInterfaceCore()->ReleaseResource(ctx->image);
74 ctx->image = 0;
77 return 1;
79 default:
80 break;
83 return 0;
86 // Allocates (if needed) a new image context which will be swapped in when
87 // drawing is complete. PSContextGetBuffer and PSContext2DSwapBuffer
88 // implemented the suggested image/graphic_2d use specified in the
89 // ppb_graphics_2d header.
90 int PSContext2DGetBuffer(PSContext2D_t* ctx) {
91 if (!ctx->bound)
92 return 0;
94 // Check if we are already holding an image
95 if (ctx->image)
96 return 1;
98 struct PP_Size size;
99 size.width = ctx->width;
100 size.height = ctx->height;
102 // Allocate a new image resource with the specified size and format, but
103 // do not ZERO out the buffer first since we will fill it.
104 PP_Resource image = PSInterfaceImageData()->Create(
105 PSGetInstanceId(), ctx->format, &size, PP_FALSE);
107 if (0 == image) {
108 PSInstanceError("Unable to create 2D image.\n");
109 return 0;
112 // Get the stride
113 struct PP_ImageDataDesc desc;
114 PSInterfaceImageData()->Describe(image, &desc);
116 ctx->image = image;
117 ctx->data = (uint32_t*)(PSInterfaceImageData()->Map(image));
118 ctx->stride = desc.stride;
119 return 1;
122 int PSContext2DSwapBuffer(PSContext2D_t* ctx) {
123 if (ctx->bound && ctx->image) {
124 PSInterfaceImageData()->Unmap(ctx->image);
125 PSInterfaceGraphics2D()->ReplaceContents(ctx->graphic_2d, ctx->image);
126 PSInterfaceGraphics2D()->Flush(ctx->graphic_2d, PP_BlockUntilComplete());
127 PSInterfaceCore()->ReleaseResource(ctx->image);
129 ctx->image = 0;
130 ctx->stride = 0;
131 ctx->data = NULL;
132 return 1;
134 return 0;