1 // Copyright 2014 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 "ppapi/cpp/compositor_layer.h" 6 7 #include "ppapi/c/pp_errors.h" 8 #include "ppapi/cpp/completion_callback.h" 9 #include "ppapi/cpp/module_impl.h" 10 #include "ppapi/cpp/var.h" 11 12 namespace pp { 13 14 namespace { 15 16 template <> const char* interface_name<PPB_CompositorLayer_0_1>() { 17 return PPB_COMPOSITORLAYER_INTERFACE_0_1; 18 } 19 20 template <> const char* interface_name<PPB_CompositorLayer_0_2>() { 21 return PPB_COMPOSITORLAYER_INTERFACE_0_2; 22 } 23 24 } // namespace 25 26 CompositorLayer::CompositorLayer() { 27 } 28 29 CompositorLayer::CompositorLayer( 30 const CompositorLayer& other) : Resource(other) { 31 } 32 33 CompositorLayer::CompositorLayer(const Resource& resource) 34 : Resource(resource) { 35 PP_DCHECK(IsCompositorLayer(resource)); 36 } 37 38 CompositorLayer::CompositorLayer(PassRef, PP_Resource resource) 39 : Resource(PASS_REF, resource) { 40 } 41 42 CompositorLayer::~CompositorLayer() { 43 } 44 45 int32_t CompositorLayer::SetColor(float red, 46 float green, 47 float blue, 48 float alpha, 49 const Size& size) { 50 if (has_interface<PPB_CompositorLayer_0_2>()) { 51 return get_interface<PPB_CompositorLayer_0_2>()->SetColor( 52 pp_resource(), red, green, blue, alpha, &size.pp_size()); 53 } 54 if (has_interface<PPB_CompositorLayer_0_1>()) { 55 return get_interface<PPB_CompositorLayer_0_1>()->SetColor( 56 pp_resource(), red, green, blue, alpha, &size.pp_size()); 57 } 58 return PP_ERROR_NOINTERFACE; 59 } 60 61 int32_t CompositorLayer::SetTexture(const Graphics3D& context, 62 uint32_t target, 63 uint32_t texture, 64 const Size& size, 65 const CompletionCallback& cc) { 66 if (has_interface<PPB_CompositorLayer_0_2>()) { 67 return get_interface<PPB_CompositorLayer_0_2>()->SetTexture( 68 pp_resource(), context.pp_resource(), target, texture, &size.pp_size(), 69 cc.pp_completion_callback()); 70 } 71 if (has_interface<PPB_CompositorLayer_0_1>()) { 72 if (target != 0x0DE1) // 0x0DE1 GL_TEXTURE_2D 73 return cc.MayForce(PP_ERROR_NOTSUPPORTED); 74 return get_interface<PPB_CompositorLayer_0_1>()->SetTexture( 75 pp_resource(), context.pp_resource(), texture, &size.pp_size(), 76 cc.pp_completion_callback()); 77 } 78 return cc.MayForce(PP_ERROR_NOINTERFACE); 79 } 80 81 int32_t CompositorLayer::SetImage(const ImageData& image, 82 const CompletionCallback& cc) { 83 if (has_interface<PPB_CompositorLayer_0_2>()) { 84 return get_interface<PPB_CompositorLayer_0_2>()->SetImage( 85 pp_resource(), image.pp_resource(), NULL, 86 cc.pp_completion_callback()); 87 } 88 if (has_interface<PPB_CompositorLayer_0_1>()) { 89 return get_interface<PPB_CompositorLayer_0_1>()->SetImage( 90 pp_resource(), image.pp_resource(), NULL, 91 cc.pp_completion_callback()); 92 } 93 return cc.MayForce(PP_ERROR_NOINTERFACE); 94 } 95 96 int32_t CompositorLayer::SetImage(const ImageData& image, 97 const Size& size, 98 const CompletionCallback& cc) { 99 if (has_interface<PPB_CompositorLayer_0_2>()) { 100 return get_interface<PPB_CompositorLayer_0_2>()->SetImage( 101 pp_resource(), image.pp_resource(), &size.pp_size(), 102 cc.pp_completion_callback()); 103 } 104 if (has_interface<PPB_CompositorLayer_0_1>()) { 105 return get_interface<PPB_CompositorLayer_0_1>()->SetImage( 106 pp_resource(), image.pp_resource(), &size.pp_size(), 107 cc.pp_completion_callback()); 108 } 109 return cc.MayForce(PP_ERROR_NOINTERFACE); 110 } 111 112 int32_t CompositorLayer::SetClipRect(const Rect& rect) { 113 if (has_interface<PPB_CompositorLayer_0_2>()) { 114 return get_interface<PPB_CompositorLayer_0_2>()->SetClipRect( 115 pp_resource(), &rect.pp_rect()); 116 } 117 if (has_interface<PPB_CompositorLayer_0_1>()) { 118 return get_interface<PPB_CompositorLayer_0_1>()->SetClipRect( 119 pp_resource(), &rect.pp_rect()); 120 } 121 return PP_ERROR_NOINTERFACE; 122 } 123 124 int32_t CompositorLayer::SetTransform(const float matrix[16]) { 125 if (has_interface<PPB_CompositorLayer_0_2>()) { 126 return get_interface<PPB_CompositorLayer_0_2>()->SetTransform( 127 pp_resource(), matrix); 128 } 129 if (has_interface<PPB_CompositorLayer_0_1>()) { 130 return get_interface<PPB_CompositorLayer_0_1>()->SetTransform( 131 pp_resource(), matrix); 132 } 133 return PP_ERROR_NOINTERFACE; 134 } 135 136 int32_t CompositorLayer::SetOpacity(float opacity) { 137 if (has_interface<PPB_CompositorLayer_0_2>()) { 138 return get_interface<PPB_CompositorLayer_0_2>()->SetOpacity( 139 pp_resource(), opacity); 140 } 141 if (has_interface<PPB_CompositorLayer_0_1>()) { 142 return get_interface<PPB_CompositorLayer_0_1>()->SetOpacity( 143 pp_resource(), opacity); 144 } 145 return PP_ERROR_NOINTERFACE; 146 } 147 148 int32_t CompositorLayer::SetBlendMode(PP_BlendMode mode) { 149 if (has_interface<PPB_CompositorLayer_0_2>()) { 150 return get_interface<PPB_CompositorLayer_0_2>()->SetBlendMode( 151 pp_resource(), mode); 152 } 153 if (has_interface<PPB_CompositorLayer_0_1>()) { 154 return get_interface<PPB_CompositorLayer_0_1>()->SetBlendMode( 155 pp_resource(), mode); 156 } 157 return PP_ERROR_NOINTERFACE; 158 } 159 160 int32_t CompositorLayer::SetSourceRect(const FloatRect& rect) { 161 if (has_interface<PPB_CompositorLayer_0_2>()) { 162 return get_interface<PPB_CompositorLayer_0_2>()->SetSourceRect( 163 pp_resource(), &rect.pp_float_rect()); 164 } 165 if (has_interface<PPB_CompositorLayer_0_1>()) { 166 return get_interface<PPB_CompositorLayer_0_1>()->SetSourceRect( 167 pp_resource(), &rect.pp_float_rect()); 168 } 169 return PP_ERROR_NOINTERFACE; 170 } 171 172 int32_t CompositorLayer::SetPremultipliedAlpha(bool premult) { 173 if (has_interface<PPB_CompositorLayer_0_2>()) { 174 return get_interface<PPB_CompositorLayer_0_2>()->SetPremultipliedAlpha( 175 pp_resource(), PP_FromBool(premult)); 176 } 177 if (has_interface<PPB_CompositorLayer_0_1>()) { 178 return get_interface<PPB_CompositorLayer_0_1>()->SetPremultipliedAlpha( 179 pp_resource(), PP_FromBool(premult)); 180 } 181 return PP_ERROR_NOINTERFACE; 182 } 183 184 bool CompositorLayer::IsCompositorLayer(const Resource& resource) { 185 if (has_interface<PPB_CompositorLayer_0_2>()) { 186 return PP_ToBool(get_interface<PPB_CompositorLayer_0_2>()-> 187 IsCompositorLayer(resource.pp_resource())); 188 } 189 if (has_interface<PPB_CompositorLayer_0_1>()) { 190 return PP_ToBool(get_interface<PPB_CompositorLayer_0_1>()-> 191 IsCompositorLayer(resource.pp_resource())); 192 } 193 return false; 194 } 195 196 } // namespace pp 197