Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 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_rect.h" | 8 #include "ppapi/c/pp_rect.h" |
| 9 #include "ppapi/c/pp_resource.h" | 9 #include "ppapi/c/pp_resource.h" |
| 10 #include "ppapi/c/pp_size.h" | 10 #include "ppapi/c/pp_size.h" |
| 11 #include "ppapi/c/ppb_core.h" | 11 #include "ppapi/c/ppb_core.h" |
| 12 #include "ppapi/c/ppb_graphics_2d.h" | 12 #include "ppapi/c/ppb_graphics_2d.h" |
| 13 #include "ppapi/c/ppb_image_data.h" | 13 #include "ppapi/c/ppb_image_data.h" |
| 14 #include "ppapi/c/ppb_instance.h" | 14 #include "ppapi/c/ppb_instance.h" |
| 15 #include "ppapi/c/ppb_view.h" | 15 #include "ppapi/c/ppb_view.h" |
| 16 #include "ppapi/cpp/image_data.h" | |
| 17 | 16 |
| 18 #include "ppapi_simple/ps.h" | 17 #include "ppapi_simple/ps.h" |
| 19 #include "ppapi_simple/ps_context_2d.h" | 18 #include "ppapi_simple/ps_context_2d.h" |
| 20 #include "ppapi_simple/ps_event.h" | 19 #include "ppapi_simple/ps_event.h" |
| 21 #include "ppapi_simple/ps_instance.h" | 20 #include "ppapi_simple/ps_instance.h" |
| 22 #include "ppapi_simple/ps_interface.h" | 21 #include "ppapi_simple/ps_interface.h" |
| 23 | 22 |
| 24 PSContext2D_t* PSContext2DAllocate(PP_ImageDataFormat format) { | 23 PSContext2D_t* PSContext2DAllocate(PP_ImageDataFormat format) { |
| 25 PSContext2D_t* ctx = (PSContext2D_t*) malloc(sizeof(PSContext2D_t)); | 24 PSContext2D_t* ctx = (PSContext2D_t*)malloc(sizeof(PSContext2D_t)); |
| 26 memset(ctx, 0, sizeof(PSContext2D_t)); | 25 memset(ctx, 0, sizeof(PSContext2D_t)); |
| 27 | 26 |
| 28 ctx->format = format; | 27 ctx->format = format; |
| 29 return ctx; | 28 return ctx; |
| 30 } | 29 } |
| 31 | 30 |
| 32 void PSContext2DFree(PSContext2D_t* ctx) { | 31 void PSContext2DFree(PSContext2D_t* ctx) { |
| 33 if (ctx->graphic_2d) { | 32 if (ctx->graphic_2d) { |
| 34 PSInterfaceCore()->ReleaseResource(ctx->graphic_2d); | 33 PSInterfaceCore()->ReleaseResource(ctx->graphic_2d); |
| 35 ctx->graphic_2d = 0; | 34 ctx->graphic_2d = 0; |
| 36 } | 35 } |
| 37 if (ctx->image) { | 36 if (ctx->image) { |
| 38 PSInterfaceCore()->ReleaseResource(ctx->image); | 37 PSInterfaceCore()->ReleaseResource(ctx->image); |
| 39 ctx->image = 0; | 38 ctx->image = 0; |
| 40 } | 39 } |
| 41 free(ctx); | 40 free(ctx); |
| 42 } | 41 } |
| 43 | 42 |
| 44 PP_ImageDataFormat PSContext2DGetNativeImageDataFormat() { | 43 PP_ImageDataFormat PSContext2DGetNativeImageDataFormat() { |
| 45 return PSInterfaceImageData()->GetNativeImageDataFormat(); | 44 return PSInterfaceImageData()->GetNativeImageDataFormat(); |
| 46 } | 45 } |
| 47 | 46 |
| 48 // Update the 2D context if the message is appropriate, returning non-zero | 47 // Update the 2D context if the message is appropriate, returning non-zero |
| 49 // if the event was consumed. | 48 // if the event was consumed. |
| 50 int PSContext2DHandleEvent(PSContext2D_t* ctx, PSEvent* event) { | 49 int PSContext2DHandleEvent(PSContext2D_t* ctx, PSEvent* event) { |
| 51 switch(event->type) { | 50 switch (event->type) { |
| 52 case PSE_INSTANCE_DIDCHANGEVIEW: { | 51 case PSE_INSTANCE_DIDCHANGEVIEW: { |
| 53 struct PP_Rect rect; | 52 struct PP_Rect rect; |
| 54 | 53 |
| 55 PSInterfaceView()->GetRect(event->as_resource, &rect); | 54 PSInterfaceView()->GetRect(event->as_resource, &rect); |
| 56 PSInterfaceCore()->ReleaseResource(ctx->graphic_2d); | 55 PSInterfaceCore()->ReleaseResource(ctx->graphic_2d); |
| 57 ctx->bound = 0; | 56 ctx->bound = 0; |
| 58 ctx->width = rect.size.width; | 57 ctx->width = rect.size.width; |
| 59 ctx->height = rect.size.height; | 58 ctx->height = rect.size.height; |
| 60 | 59 |
| 61 // Create an opaque graphic context of the specified size. | 60 // Create an opaque graphic context of the specified size. |
| 62 ctx->graphic_2d = | 61 ctx->graphic_2d = PSInterfaceGraphics2D()->Create(PSGetInstanceId(), |
| 63 PSInterfaceGraphics2D()->Create(PSGetInstanceId(), &rect.size, | 62 &rect.size, PP_TRUE); |
| 64 PP_TRUE); | |
| 65 | 63 |
| 66 // Bind the context to so that draws will be visible. | 64 // Bind the context to so that draws will be visible. |
| 67 if (ctx->graphic_2d) { | 65 if (ctx->graphic_2d) { |
| 68 ctx->bound = | 66 ctx->bound = PSInterfaceInstance()->BindGraphics(PSGetInstanceId(), |
| 69 PSInterfaceInstance()->BindGraphics(PSGetInstanceId(), | 67 ctx->graphic_2d); |
| 70 ctx->graphic_2d); | |
| 71 } | 68 } |
| 72 | 69 |
| 73 // Typically this resource would not be allocated yet, but just in case | 70 // Typically this resource would not be allocated yet, but just in case |
| 74 // throw it away, to force a new allocation when GetBuffer is called. | 71 // throw it away, to force a new allocation when GetBuffer is called. |
| 75 if (ctx->image) { | 72 if (ctx->image) { |
| 76 PSInterfaceCore()->ReleaseResource(ctx->image); | 73 PSInterfaceCore()->ReleaseResource(ctx->image); |
| 77 ctx->image = 0; | 74 ctx->image = 0; |
| 78 } | 75 } |
| 79 | 76 |
| 80 return 1; | 77 return 1; |
| 81 } | 78 } |
| 82 default: break; | 79 default: |
| 80 break; | |
| 83 } | 81 } |
| 84 | 82 |
| 85 return 0; | 83 return 0; |
| 86 } | 84 } |
| 87 | 85 |
| 88 | |
| 89 // Allocates (if needed) a new image context which will be swapped in when | 86 // Allocates (if needed) a new image context which will be swapped in when |
| 90 // drawing is complete. PSContextGetBuffer and PSContext2DSwapBuffer | 87 // drawing is complete. PSContextGetBuffer and PSContext2DSwapBuffer |
| 91 // implemented the suggested image/graphic_2d use specified in the | 88 // implemented the suggested image/graphic_2d use specified in the |
| 92 // ppb_graphics_2d header. | 89 // ppb_graphics_2d header. |
| 93 int PSContext2DGetBuffer(PSContext2D_t* ctx) { | 90 int PSContext2DGetBuffer(PSContext2D_t* ctx) { |
| 94 if (!ctx->bound) return 0; | 91 if (!ctx->bound) |
| 92 return 0; | |
| 95 | 93 |
| 96 // Check if we are already holding an image | 94 // Check if we are already holding an image |
| 97 if (ctx->image) return 1; | 95 if (ctx->image) |
| 96 return 1; | |
| 98 | 97 |
| 99 PP_Size size; | 98 struct PP_Size size; |
| 100 size.width = ctx->width; | 99 size.width = ctx->width; |
| 101 size.height = ctx->height; | 100 size.height = ctx->height; |
| 102 | 101 |
| 103 // Allocate a new image resource with the specified size and format, but | 102 // Allocate a new image resource with the specified size and format, but |
| 104 // do not ZERO out the buffer first since we will fill it. | 103 // do not ZERO out the buffer first since we will fill it. |
| 105 PP_Resource image = | 104 PP_Resource image = PSInterfaceImageData()->Create( |
| 106 PSInterfaceImageData()->Create(PSGetInstanceId(), ctx->format, &size, | 105 PSGetInstanceId(), ctx->format, &size, PP_FALSE); |
| 107 PP_FALSE); | |
| 108 | 106 |
| 109 if (0 == image) { | 107 if (0 == image) { |
| 110 PSInstance::GetInstance()->Error("Unable to create 2D image.\n"); | 108 PSInstanceError("Unable to create 2D image.\n"); |
| 111 return 0; | 109 return 0; |
| 112 } | 110 } |
| 113 | 111 |
| 114 // Get the stride | 112 // Get the stride |
| 115 struct PP_ImageDataDesc desc; | 113 struct PP_ImageDataDesc desc; |
| 116 PSInterfaceImageData()->Describe(image, &desc); | 114 PSInterfaceImageData()->Describe(image, &desc); |
| 117 | 115 |
| 118 ctx->image = image; | 116 ctx->image = image; |
| 119 ctx->data = static_cast<uint32_t*>(PSInterfaceImageData()->Map(image)); | 117 ctx->data = (uint32_t*)(PSInterfaceImageData()->Map(image)); |
| 120 ctx->stride = desc.stride; | 118 ctx->stride = desc.stride; |
| 121 return 1; | 119 return 1; |
| 122 } | 120 } |
| 123 | 121 |
| 124 int PSContext2DSwapBuffer(PSContext2D_t* ctx) { | 122 int PSContext2DSwapBuffer(PSContext2D_t* ctx) { |
| 125 if (ctx->bound && ctx->image) { | 123 if (ctx->bound && ctx->image) { |
| 126 PSInterfaceImageData()->Unmap(ctx->image); | 124 PSInterfaceImageData()->Unmap(ctx->image); |
| 127 PSInterfaceGraphics2D()->ReplaceContents(ctx->graphic_2d, ctx->image); | 125 PSInterfaceGraphics2D()->ReplaceContents(ctx->graphic_2d, ctx->image); |
| 128 PSInterfaceGraphics2D()->Flush(ctx->graphic_2d, PP_BlockUntilComplete()); | 126 PSInterfaceGraphics2D()->Flush(ctx->graphic_2d, PP_BlockUntilComplete()); |
| 129 PSInterfaceCore()->ReleaseResource(ctx->image); | 127 PSInterfaceCore()->ReleaseResource(ctx->image); |
| 130 | 128 |
| 131 ctx->image = 0; | 129 ctx->image = 0; |
| 132 ctx->stride = 0; | 130 ctx->stride = 0; |
| 133 ctx->data = NULL; | 131 ctx->data = NULL; |
| 134 return 1; | 132 return 1; |
| 135 } | 133 } |
| 136 return 0; | 134 return 0; |
| 137 } | 135 } |
| 138 | 136 |
|
Sam Clegg
2015/02/19 21:24:20
nit: Remove trailing newline
binji
2015/02/20 17:33:48
Done.
| |
| OLD | NEW |