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. */
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
));
31 void PSContext2DFree(PSContext2D_t
* ctx
) {
32 if (ctx
->graphic_2d
) {
33 PSInterfaceCore()->ReleaseResource(ctx
->graphic_2d
);
37 PSInterfaceCore()->ReleaseResource(ctx
->image
);
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
: {
54 PSInterfaceView()->GetRect(event
->as_resource
, &rect
);
55 PSInterfaceCore()->ReleaseResource(ctx
->graphic_2d
);
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(),
64 // Bind the context to so that draws will be visible.
65 if (ctx
->graphic_2d
) {
66 ctx
->bound
= PSInterfaceInstance()->BindGraphics(PSGetInstanceId(),
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.
73 PSInterfaceCore()->ReleaseResource(ctx
->image
);
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
) {
94 // Check if we are already holding an image
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
);
108 PSInstanceError("Unable to create 2D image.\n");
113 struct PP_ImageDataDesc desc
;
114 PSInterfaceImageData()->Describe(image
, &desc
);
117 ctx
->data
= (uint32_t*)(PSInterfaceImageData()->Map(image
));
118 ctx
->stride
= desc
.stride
;
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
);