OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "webkit/glue/plugins/pepper_image_data.h" |
| 6 |
| 7 #include "base/scoped_ptr.h" |
| 8 #include "skia/ext/platform_canvas.h" |
| 9 #include "third_party/ppapi/c/pp_instance.h" |
| 10 #include "third_party/ppapi/c/pp_module.h" |
| 11 #include "third_party/ppapi/c/pp_resource.h" |
| 12 #include "third_party/ppapi/c/ppb_image_data.h" |
| 13 #include "webkit/glue/plugins/pepper_plugin_instance.h" |
| 14 #include "webkit/glue/plugins/pepper_plugin_module.h" |
| 15 #include "webkit/glue/plugins/pepper_resource_tracker.h" |
| 16 |
| 17 namespace pepper { |
| 18 |
| 19 namespace { |
| 20 |
| 21 ImageData* ResourceAsImageData(PP_Resource resource) { |
| 22 scoped_refptr<Resource> image_resource = |
| 23 ResourceTracker::Get()->GetResource(resource); |
| 24 if (!image_resource.get()) |
| 25 return NULL; |
| 26 return image_resource->AsImageData(); |
| 27 } |
| 28 |
| 29 PP_Resource Create(PP_Module module_id, |
| 30 PP_ImageDataFormat format, |
| 31 int32_t width, |
| 32 int32_t height) { |
| 33 PluginModule* module = PluginModule::FromPPModule(module_id); |
| 34 if (!module) |
| 35 return NullPPResource(); |
| 36 |
| 37 scoped_refptr<ImageData> data(new ImageData(module)); |
| 38 if (!data->Init(format, width, height)) |
| 39 return NullPPResource(); |
| 40 data->AddRef(); // AddRef for the caller. |
| 41 |
| 42 return data->GetResource(); |
| 43 } |
| 44 |
| 45 bool IsImageData(PP_Resource resource) { |
| 46 scoped_refptr<Resource> image_resource = |
| 47 ResourceTracker::Get()->GetResource(resource); |
| 48 if (!image_resource.get()) |
| 49 return false; |
| 50 return !!image_resource->AsImageData(); |
| 51 } |
| 52 |
| 53 bool Describe(PP_Resource resource, |
| 54 PP_ImageDataDesc* desc) { |
| 55 // Give predictable values on failure. |
| 56 memset(desc, 0, sizeof(PP_ImageDataDesc)); |
| 57 |
| 58 ImageData* image_data = ResourceAsImageData(resource); |
| 59 if (!image_data) |
| 60 return false; |
| 61 image_data->Describe(desc); |
| 62 return true; |
| 63 } |
| 64 |
| 65 void* Map(PP_Resource resource) { |
| 66 ImageData* image_data = ResourceAsImageData(resource); |
| 67 if (!image_data) |
| 68 return NULL; |
| 69 return image_data->Map(); |
| 70 } |
| 71 |
| 72 void Unmap(PP_Resource resource) { |
| 73 ImageData* image_data = ResourceAsImageData(resource); |
| 74 if (!image_data) |
| 75 return; |
| 76 return image_data->Unmap(); |
| 77 } |
| 78 |
| 79 const PPB_ImageData ppb_imagedata = { |
| 80 &Create, |
| 81 &IsImageData, |
| 82 &Describe, |
| 83 &Map, |
| 84 &Unmap, |
| 85 }; |
| 86 |
| 87 } // namespace |
| 88 |
| 89 ImageData::ImageData(PluginModule* module) |
| 90 : Resource(module), |
| 91 width_(0), |
| 92 height_(0) { |
| 93 } |
| 94 |
| 95 ImageData::~ImageData() { |
| 96 } |
| 97 |
| 98 // static |
| 99 const PPB_ImageData* ImageData::GetInterface() { |
| 100 return &ppb_imagedata; |
| 101 } |
| 102 |
| 103 bool ImageData::Init(PP_ImageDataFormat format, |
| 104 int width, |
| 105 int height) { |
| 106 // TODO(brettw) this should be called only on the main thread! |
| 107 platform_image_.reset( |
| 108 module()->GetSomeInstance()->delegate()->CreateImage2D(width, height)); |
| 109 width_ = width; |
| 110 height_ = height; |
| 111 return !!platform_image_.get(); |
| 112 } |
| 113 |
| 114 void ImageData::Describe(PP_ImageDataDesc* desc) const { |
| 115 desc->format = PP_IMAGEDATAFORMAT_BGRA_PREMUL; |
| 116 desc->width = width_; |
| 117 desc->height = height_; |
| 118 desc->stride = width_ * 4; |
| 119 } |
| 120 |
| 121 void* ImageData::Map() { |
| 122 if (!mapped_canvas_.get()) { |
| 123 mapped_canvas_.reset(platform_image_->Map()); |
| 124 if (!mapped_canvas_.get()) |
| 125 return NULL; |
| 126 } |
| 127 const SkBitmap& bitmap = |
| 128 mapped_canvas_->getTopPlatformDevice().accessBitmap(true); |
| 129 |
| 130 // Our platform bitmaps are set to opaque by default, which we don't want. |
| 131 const_cast<SkBitmap&>(bitmap).setIsOpaque(false); |
| 132 |
| 133 bitmap.lockPixels(); |
| 134 return bitmap.getAddr32(0, 0); |
| 135 } |
| 136 |
| 137 void ImageData::Unmap() { |
| 138 // This is currently unimplemented, which is OK. The data will just always |
| 139 // be around once it's mapped. Chrome's TransportDIB isn't currently |
| 140 // unmappable without freeing it, but this may be something we want to support |
| 141 // in the future to save some memory. |
| 142 } |
| 143 |
| 144 const SkBitmap& ImageData::GetMappedBitmap() const { |
| 145 return mapped_canvas_->getTopPlatformDevice().accessBitmap(false); |
| 146 } |
| 147 |
| 148 } // namespace pepper |
OLD | NEW |