This source file includes following definitions.
- PSContext2DAllocate
- PSContext2DFree
- PSContext2DGetNativeImageDataFormat
- PSContext2DHandleEvent
- PSContext2DGetBuffer
- PSContext2DSwapBuffer
#include <stdlib.h>
#include <string.h>
#include "ppapi/c/pp_rect.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/pp_size.h"
#include "ppapi/c/ppb_core.h"
#include "ppapi/c/ppb_graphics_2d.h"
#include "ppapi/c/ppb_image_data.h"
#include "ppapi/c/ppb_instance.h"
#include "ppapi/c/ppb_view.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi_simple/ps.h"
#include "ppapi_simple/ps_context_2d.h"
#include "ppapi_simple/ps_event.h"
#include "ppapi_simple/ps_instance.h"
#include "ppapi_simple/ps_interface.h"
PSContext2D_t* PSContext2DAllocate(PP_ImageDataFormat format) {
PSContext2D_t* ctx = (PSContext2D_t*) malloc(sizeof(PSContext2D_t));
memset(ctx, 0, sizeof(PSContext2D_t));
ctx->format = format;
return ctx;
}
void PSContext2DFree(PSContext2D_t* ctx) {
if (ctx->graphic_2d) {
PSInterfaceCore()->ReleaseResource(ctx->graphic_2d);
ctx->graphic_2d = 0;
}
if (ctx->image) {
PSInterfaceCore()->ReleaseResource(ctx->image);
ctx->image = 0;
}
free(ctx);
}
PP_ImageDataFormat PSContext2DGetNativeImageDataFormat() {
return PSInterfaceImageData()->GetNativeImageDataFormat();
}
int PSContext2DHandleEvent(PSContext2D_t* ctx, PSEvent* event) {
switch(event->type) {
case PSE_INSTANCE_DIDCHANGEVIEW: {
struct PP_Rect rect;
PSInterfaceView()->GetRect(event->as_resource, &rect);
PSInterfaceCore()->ReleaseResource(ctx->graphic_2d);
ctx->bound = 0;
ctx->width = rect.size.width;
ctx->height = rect.size.height;
ctx->graphic_2d =
PSInterfaceGraphics2D()->Create(PSGetInstanceId(), &rect.size,
PP_TRUE);
if (ctx->graphic_2d) {
ctx->bound =
PSInterfaceInstance()->BindGraphics(PSGetInstanceId(),
ctx->graphic_2d);
}
if (ctx->image) {
PSInterfaceCore()->ReleaseResource(ctx->image);
ctx->image = 0;
}
return 1;
}
default: break;
}
return 0;
}
int PSContext2DGetBuffer(PSContext2D_t* ctx) {
if (!ctx->bound) return 0;
if (ctx->image) return 1;
PP_Size size;
size.width = ctx->width;
size.height = ctx->height;
PP_Resource image =
PSInterfaceImageData()->Create(PSGetInstanceId(), ctx->format, &size,
PP_FALSE);
if (0 == image) {
PSInstance::GetInstance()->Error("Unable to create 2D image.\n");
return 0;
}
struct PP_ImageDataDesc desc;
PSInterfaceImageData()->Describe(image, &desc);
ctx->image = image;
ctx->data = static_cast<uint32_t*>(PSInterfaceImageData()->Map(image));
ctx->stride = desc.stride;
return 1;
}
int PSContext2DSwapBuffer(PSContext2D_t* ctx) {
if (ctx->bound && ctx->image) {
PSInterfaceImageData()->Unmap(ctx->image);
PSInterfaceGraphics2D()->ReplaceContents(ctx->graphic_2d, ctx->image);
PSInterfaceGraphics2D()->Flush(ctx->graphic_2d, PP_BlockUntilComplete());
PSInterfaceCore()->ReleaseResource(ctx->image);
ctx->image = 0;
ctx->stride = 0;
ctx->data = NULL;
return 1;
}
return 0;
}