| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/plugins/ppapi/ppb_image_data_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "skia/ext/platform_canvas.h" | 12 #include "skia/ext/platform_canvas.h" |
| 13 #include "ppapi/c/pp_instance.h" | 13 #include "ppapi/c/pp_instance.h" |
| 14 #include "ppapi/c/pp_resource.h" | 14 #include "ppapi/c/pp_resource.h" |
| 15 #include "ppapi/c/ppb_image_data.h" | 15 #include "ppapi/c/ppb_image_data.h" |
| 16 #include "ppapi/c/trusted/ppb_image_data_trusted.h" | 16 #include "ppapi/c/trusted/ppb_image_data_trusted.h" |
| 17 #include "ppapi/thunk/thunk.h" |
| 17 #include "third_party/skia/include/core/SkColorPriv.h" | 18 #include "third_party/skia/include/core/SkColorPriv.h" |
| 18 #include "webkit/plugins/ppapi/common.h" | 19 #include "webkit/plugins/ppapi/common.h" |
| 19 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 20 | 21 |
| 21 namespace webkit { | 22 namespace webkit { |
| 22 namespace ppapi { | 23 namespace ppapi { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 PP_ImageDataFormat GetNativeImageDataFormat() { | |
| 27 return PPB_ImageData_Impl::GetNativeImageDataFormat(); | |
| 28 } | |
| 29 | |
| 30 PP_Bool IsImageDataFormatSupported(PP_ImageDataFormat format) { | |
| 31 return BoolToPPBool(PPB_ImageData_Impl::IsImageDataFormatSupported(format)); | |
| 32 } | |
| 33 | |
| 34 PP_Resource Create(PP_Instance instance_id, | |
| 35 PP_ImageDataFormat format, | |
| 36 const PP_Size* size, | |
| 37 PP_Bool init_to_zero) { | |
| 38 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | |
| 39 if (!instance) | |
| 40 return 0; | |
| 41 | |
| 42 scoped_refptr<PPB_ImageData_Impl> data(new PPB_ImageData_Impl(instance)); | |
| 43 if (!data->Init(format, | |
| 44 size->width, | |
| 45 size->height, | |
| 46 PPBoolToBool(init_to_zero))) { | |
| 47 return 0; | |
| 48 } | |
| 49 | |
| 50 return data->GetReference(); | |
| 51 } | |
| 52 | |
| 53 PP_Bool IsImageData(PP_Resource resource) { | |
| 54 return BoolToPPBool(!!Resource::GetAs<PPB_ImageData_Impl>(resource)); | |
| 55 } | |
| 56 | |
| 57 PP_Bool Describe(PP_Resource resource, PP_ImageDataDesc* desc) { | |
| 58 // Give predictable values on failure. | |
| 59 memset(desc, 0, sizeof(PP_ImageDataDesc)); | |
| 60 | |
| 61 scoped_refptr<PPB_ImageData_Impl> image_data( | |
| 62 Resource::GetAs<PPB_ImageData_Impl>(resource)); | |
| 63 if (!image_data) | |
| 64 return PP_FALSE; | |
| 65 image_data->Describe(desc); | |
| 66 return PP_TRUE; | |
| 67 } | |
| 68 | |
| 69 void* Map(PP_Resource resource) { | |
| 70 scoped_refptr<PPB_ImageData_Impl> image_data( | |
| 71 Resource::GetAs<PPB_ImageData_Impl>(resource)); | |
| 72 if (!image_data) | |
| 73 return NULL; | |
| 74 return image_data->Map(); | |
| 75 } | |
| 76 | |
| 77 void Unmap(PP_Resource resource) { | |
| 78 scoped_refptr<PPB_ImageData_Impl> image_data( | |
| 79 Resource::GetAs<PPB_ImageData_Impl>(resource)); | |
| 80 if (image_data) | |
| 81 image_data->Unmap(); | |
| 82 } | |
| 83 | |
| 84 int32_t GetSharedMemory(PP_Resource resource, | 27 int32_t GetSharedMemory(PP_Resource resource, |
| 85 int* handle, | 28 int* handle, |
| 86 uint32_t* byte_count) { | 29 uint32_t* byte_count) { |
| 87 scoped_refptr<PPB_ImageData_Impl> image_data( | 30 scoped_refptr<PPB_ImageData_Impl> image_data( |
| 88 Resource::GetAs<PPB_ImageData_Impl>(resource)); | 31 Resource::GetAs<PPB_ImageData_Impl>(resource)); |
| 89 if (image_data) { | 32 if (image_data) { |
| 90 *handle = image_data->GetSharedMemoryHandle(byte_count); | 33 *handle = image_data->GetSharedMemoryHandle(byte_count); |
| 91 return PP_OK; | 34 return PP_OK; |
| 92 } | 35 } |
| 93 return PP_ERROR_BADRESOURCE; | 36 return PP_ERROR_BADRESOURCE; |
| 94 } | 37 } |
| 95 | 38 |
| 96 const PPB_ImageData ppb_imagedata = { | |
| 97 &GetNativeImageDataFormat, | |
| 98 &IsImageDataFormatSupported, | |
| 99 &Create, | |
| 100 &IsImageData, | |
| 101 &Describe, | |
| 102 &Map, | |
| 103 &Unmap, | |
| 104 }; | |
| 105 | |
| 106 const PPB_ImageDataTrusted ppb_imagedata_trusted = { | 39 const PPB_ImageDataTrusted ppb_imagedata_trusted = { |
| 107 &GetSharedMemory, | 40 &GetSharedMemory, |
| 108 }; | 41 }; |
| 109 | 42 |
| 110 } // namespace | 43 } // namespace |
| 111 | 44 |
| 112 PPB_ImageData_Impl::PPB_ImageData_Impl(PluginInstance* instance) | 45 PPB_ImageData_Impl::PPB_ImageData_Impl(PluginInstance* instance) |
| 113 : Resource(instance), | 46 : Resource(instance), |
| 114 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), | 47 format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), |
| 115 width_(0), | 48 width_(0), |
| 116 height_(0) { | 49 height_(0) { |
| 117 } | 50 } |
| 118 | 51 |
| 119 PPB_ImageData_Impl::~PPB_ImageData_Impl() { | 52 PPB_ImageData_Impl::~PPB_ImageData_Impl() { |
| 120 } | 53 } |
| 121 | 54 |
| 122 // static | 55 // static |
| 123 const PPB_ImageData* PPB_ImageData_Impl::GetInterface() { | 56 const PPB_ImageData* PPB_ImageData_Impl::GetInterface() { |
| 124 return &ppb_imagedata; | 57 return ::ppapi::thunk::GetPPB_ImageData_Thunk(); |
| 125 } | 58 } |
| 126 | 59 |
| 127 // static | 60 // static |
| 128 const PPB_ImageDataTrusted* PPB_ImageData_Impl::GetTrustedInterface() { | 61 const PPB_ImageDataTrusted* PPB_ImageData_Impl::GetTrustedInterface() { |
| 129 return &ppb_imagedata_trusted; | 62 return &ppb_imagedata_trusted; |
| 130 } | 63 } |
| 131 | 64 |
| 65 ::ppapi::thunk::PPB_ImageData_API* PPB_ImageData_Impl::AsImageData_API() { |
| 66 return this; |
| 67 } |
| 68 |
| 132 PPB_ImageData_Impl* PPB_ImageData_Impl::AsPPB_ImageData_Impl() { | 69 PPB_ImageData_Impl* PPB_ImageData_Impl::AsPPB_ImageData_Impl() { |
| 133 return this; | 70 return this; |
| 134 } | 71 } |
| 135 | 72 |
| 136 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, | 73 bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, |
| 137 int width, int height, | 74 int width, int height, |
| 138 bool init_to_zero) { | 75 bool init_to_zero) { |
| 139 // TODO(brettw) this should be called only on the main thread! | 76 // TODO(brettw) this should be called only on the main thread! |
| 140 // TODO(brettw) use init_to_zero when we implement caching. | 77 // TODO(brettw) use init_to_zero when we implement caching. |
| 141 if (!IsImageDataFormatSupported(format)) | 78 if (!IsImageDataFormatSupported(format)) |
| 142 return false; // Only support this one format for now. | 79 return false; // Only support this one format for now. |
| 143 if (width <= 0 || height <= 0) | 80 if (width <= 0 || height <= 0) |
| 144 return false; | 81 return false; |
| 145 if (static_cast<int64>(width) * static_cast<int64>(height) >= | 82 if (static_cast<int64>(width) * static_cast<int64>(height) >= |
| 146 std::numeric_limits<int32>::max()) | 83 std::numeric_limits<int32>::max()) |
| 147 return false; // Prevent overflow of signed 32-bit ints. | 84 return false; // Prevent overflow of signed 32-bit ints. |
| 148 | 85 |
| 149 platform_image_.reset( | 86 platform_image_.reset( |
| 150 instance()->delegate()->CreateImage2D(width, height)); | 87 instance()->delegate()->CreateImage2D(width, height)); |
| 151 format_ = format; | 88 format_ = format; |
| 152 width_ = width; | 89 width_ = width; |
| 153 height_ = height; | 90 height_ = height; |
| 154 return !!platform_image_.get(); | 91 return !!platform_image_.get(); |
| 155 } | 92 } |
| 156 | 93 |
| 157 void PPB_ImageData_Impl::Describe(PP_ImageDataDesc* desc) const { | 94 PP_Bool PPB_ImageData_Impl::Describe(PP_ImageDataDesc* desc) { |
| 158 desc->format = format_; | 95 desc->format = format_; |
| 159 desc->size.width = width_; | 96 desc->size.width = width_; |
| 160 desc->size.height = height_; | 97 desc->size.height = height_; |
| 161 desc->stride = width_ * 4; | 98 desc->stride = width_ * 4; |
| 99 return PP_TRUE; |
| 162 } | 100 } |
| 163 | 101 |
| 164 void* PPB_ImageData_Impl::Map() { | 102 void* PPB_ImageData_Impl::Map() { |
| 165 if (!mapped_canvas_.get()) { | 103 if (!mapped_canvas_.get()) { |
| 166 mapped_canvas_.reset(platform_image_->Map()); | 104 mapped_canvas_.reset(platform_image_->Map()); |
| 167 if (!mapped_canvas_.get()) | 105 if (!mapped_canvas_.get()) |
| 168 return NULL; | 106 return NULL; |
| 169 } | 107 } |
| 170 const SkBitmap& bitmap = | 108 const SkBitmap& bitmap = |
| 171 mapped_canvas_->getTopPlatformDevice().accessBitmap(true); | 109 mapped_canvas_->getTopPlatformDevice().accessBitmap(true); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 198 swap(other->platform_image_, platform_image_); | 136 swap(other->platform_image_, platform_image_); |
| 199 swap(other->mapped_canvas_, mapped_canvas_); | 137 swap(other->mapped_canvas_, mapped_canvas_); |
| 200 std::swap(other->format_, format_); | 138 std::swap(other->format_, format_); |
| 201 std::swap(other->width_, width_); | 139 std::swap(other->width_, width_); |
| 202 std::swap(other->height_, height_); | 140 std::swap(other->height_, height_); |
| 203 } | 141 } |
| 204 | 142 |
| 205 } // namespace ppapi | 143 } // namespace ppapi |
| 206 } // namespace webkit | 144 } // namespace webkit |
| 207 | 145 |
| OLD | NEW |