Android NTP: Most visited tile clicks should have PageTransition.AUTO_BOOKMARK
[chromium-blink-merge.git] / ppapi / cpp / video_decoder.cc
blob6881e7478d1d75ad4ab39d25067f505078d0845d
1 // Copyright (c) 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.
5 #include "ppapi/cpp/video_decoder.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/ppb_video_decoder.h"
9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/cpp/instance_handle.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/cpp/module_impl.h"
14 namespace pp {
16 namespace {
18 template <>
19 const char* interface_name<PPB_VideoDecoder_0_1>() {
20 return PPB_VIDEODECODER_INTERFACE_0_1;
23 template <>
24 const char* interface_name<PPB_VideoDecoder_0_2>() {
25 return PPB_VIDEODECODER_INTERFACE_0_2;
28 template <>
29 const char* interface_name<PPB_VideoDecoder_1_0>() {
30 return PPB_VIDEODECODER_INTERFACE_1_0;
33 template <>
34 const char* interface_name<PPB_VideoDecoder_1_1>() {
35 return PPB_VIDEODECODER_INTERFACE_1_1;
38 // This struct is used to adapt CompletionCallbackWithOutput<PP_VideoPicture> to
39 // the pre-1.0 APIs, which return PP_VideoPicture_0_1. This struct is allocated
40 // on the heap, and deleted in CallbackConverter.
41 struct CallbackData_0_1 {
42 explicit CallbackData_0_1(
43 const CompletionCallbackWithOutput<PP_VideoPicture>& cc)
44 : original_picture(cc.output()),
45 original_callback(cc.pp_completion_callback()) {}
46 PP_VideoPicture_0_1 picture;
47 PP_VideoPicture* original_picture;
48 PP_CompletionCallback original_callback;
51 // Convert a 1.0 style callback to pre-1.0 callback.
52 void CallbackConverter(void* user_data, int32_t result) {
53 CallbackData_0_1* data = static_cast<CallbackData_0_1*>(user_data);
54 if (result == PP_OK) {
55 PP_VideoPicture_0_1* picture = &data->picture;
56 PP_VideoPicture* original_picture = data->original_picture;
57 original_picture->decode_id = picture->decode_id;
58 original_picture->texture_id = picture->texture_id;
59 original_picture->texture_target = picture->texture_target;
60 original_picture->texture_size = picture->texture_size;
61 // Set visible_rect to the entire picture.
62 original_picture->visible_rect = PP_MakeRectFromXYWH(
63 0, 0, picture->texture_size.width, picture->texture_size.height);
66 // Now execute the original callback.
67 PP_RunCompletionCallback(&data->original_callback, result);
68 delete data;
71 } // namespace
73 VideoDecoder::VideoDecoder() {
76 VideoDecoder::VideoDecoder(const InstanceHandle& instance) {
77 if (has_interface<PPB_VideoDecoder_1_1>()) {
78 PassRefFromConstructor(
79 get_interface<PPB_VideoDecoder_1_1>()->Create(instance.pp_instance()));
80 } else if (has_interface<PPB_VideoDecoder_1_0>()) {
81 PassRefFromConstructor(
82 get_interface<PPB_VideoDecoder_1_0>()->Create(instance.pp_instance()));
83 } else if (has_interface<PPB_VideoDecoder_0_2>()) {
84 PassRefFromConstructor(
85 get_interface<PPB_VideoDecoder_0_2>()->Create(instance.pp_instance()));
86 } else if (has_interface<PPB_VideoDecoder_0_1>()) {
87 PassRefFromConstructor(
88 get_interface<PPB_VideoDecoder_0_1>()->Create(instance.pp_instance()));
92 VideoDecoder::VideoDecoder(const VideoDecoder& other) : Resource(other) {
95 int32_t VideoDecoder::Initialize(const Graphics3D& context,
96 PP_VideoProfile profile,
97 PP_HardwareAcceleration acceleration,
98 uint32_t min_picture_count,
99 const CompletionCallback& cc) {
100 if (has_interface<PPB_VideoDecoder_1_1>()) {
101 return get_interface<PPB_VideoDecoder_1_1>()->Initialize(
102 pp_resource(), context.pp_resource(), profile, acceleration,
103 min_picture_count, cc.pp_completion_callback());
105 if (has_interface<PPB_VideoDecoder_1_0>()) {
106 if (min_picture_count != 0)
107 return cc.MayForce(PP_ERROR_NOTSUPPORTED);
108 return get_interface<PPB_VideoDecoder_1_0>()->Initialize(
109 pp_resource(), context.pp_resource(), profile, acceleration,
110 cc.pp_completion_callback());
112 if (has_interface<PPB_VideoDecoder_0_2>()) {
113 if (min_picture_count != 0)
114 return cc.MayForce(PP_ERROR_NOTSUPPORTED);
115 return get_interface<PPB_VideoDecoder_0_2>()->Initialize(
116 pp_resource(), context.pp_resource(), profile, acceleration,
117 cc.pp_completion_callback());
119 if (has_interface<PPB_VideoDecoder_0_1>()) {
120 if (min_picture_count != 0)
121 return cc.MayForce(PP_ERROR_NOTSUPPORTED);
122 if (acceleration == PP_HARDWAREACCELERATION_NONE)
123 return cc.MayForce(PP_ERROR_NOTSUPPORTED);
124 return get_interface<PPB_VideoDecoder_0_1>()->Initialize(
125 pp_resource(),
126 context.pp_resource(),
127 profile,
128 acceleration == PP_HARDWAREACCELERATION_WITHFALLBACK
129 ? PP_TRUE
130 : PP_FALSE,
131 cc.pp_completion_callback());
133 return cc.MayForce(PP_ERROR_NOINTERFACE);
136 int32_t VideoDecoder::Decode(uint32_t decode_id,
137 uint32_t size,
138 const void* buffer,
139 const CompletionCallback& cc) {
140 if (has_interface<PPB_VideoDecoder_1_0>()) {
141 return get_interface<PPB_VideoDecoder_1_0>()->Decode(
142 pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
144 if (has_interface<PPB_VideoDecoder_0_2>()) {
145 return get_interface<PPB_VideoDecoder_0_2>()->Decode(
146 pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
148 if (has_interface<PPB_VideoDecoder_0_1>()) {
149 return get_interface<PPB_VideoDecoder_0_1>()->Decode(
150 pp_resource(), decode_id, size, buffer, cc.pp_completion_callback());
152 return cc.MayForce(PP_ERROR_NOINTERFACE);
155 int32_t VideoDecoder::GetPicture(
156 const CompletionCallbackWithOutput<PP_VideoPicture>& cc) {
157 if (has_interface<PPB_VideoDecoder_1_0>()) {
158 return get_interface<PPB_VideoDecoder_1_0>()->GetPicture(
159 pp_resource(), cc.output(), cc.pp_completion_callback());
161 if (has_interface<PPB_VideoDecoder_0_2>()) {
162 // Data for our callback wrapper. The callback handler will delete it.
163 CallbackData_0_1* data = new CallbackData_0_1(cc);
164 return get_interface<PPB_VideoDecoder_0_2>()->GetPicture(
165 pp_resource(), &data->picture,
166 PP_MakeCompletionCallback(&CallbackConverter, data));
168 if (has_interface<PPB_VideoDecoder_0_1>()) {
169 // Data for our callback wrapper. The callback handler will delete it.
170 CallbackData_0_1* data = new CallbackData_0_1(cc);
171 return get_interface<PPB_VideoDecoder_0_1>()->GetPicture(
172 pp_resource(), &data->picture,
173 PP_MakeCompletionCallback(&CallbackConverter, data));
175 return cc.MayForce(PP_ERROR_NOINTERFACE);
178 void VideoDecoder::RecyclePicture(const PP_VideoPicture& picture) {
179 if (has_interface<PPB_VideoDecoder_1_0>()) {
180 get_interface<PPB_VideoDecoder_1_0>()->RecyclePicture(pp_resource(),
181 &picture);
182 } else if (has_interface<PPB_VideoDecoder_0_2>()) {
183 get_interface<PPB_VideoDecoder_0_2>()->RecyclePicture(pp_resource(),
184 &picture);
185 } else if (has_interface<PPB_VideoDecoder_0_1>()) {
186 get_interface<PPB_VideoDecoder_0_1>()->RecyclePicture(pp_resource(),
187 &picture);
191 int32_t VideoDecoder::Flush(const CompletionCallback& cc) {
192 if (has_interface<PPB_VideoDecoder_1_0>()) {
193 return get_interface<PPB_VideoDecoder_1_0>()->Flush(
194 pp_resource(), cc.pp_completion_callback());
196 if (has_interface<PPB_VideoDecoder_0_2>()) {
197 return get_interface<PPB_VideoDecoder_0_2>()->Flush(
198 pp_resource(), cc.pp_completion_callback());
200 if (has_interface<PPB_VideoDecoder_0_1>()) {
201 return get_interface<PPB_VideoDecoder_0_1>()->Flush(
202 pp_resource(), cc.pp_completion_callback());
204 return cc.MayForce(PP_ERROR_NOINTERFACE);
207 int32_t VideoDecoder::Reset(const CompletionCallback& cc) {
208 if (has_interface<PPB_VideoDecoder_1_0>()) {
209 return get_interface<PPB_VideoDecoder_1_0>()->Reset(
210 pp_resource(), cc.pp_completion_callback());
212 if (has_interface<PPB_VideoDecoder_0_2>()) {
213 return get_interface<PPB_VideoDecoder_0_2>()->Reset(
214 pp_resource(), cc.pp_completion_callback());
216 if (has_interface<PPB_VideoDecoder_0_1>()) {
217 return get_interface<PPB_VideoDecoder_0_1>()->Reset(
218 pp_resource(), cc.pp_completion_callback());
220 return cc.MayForce(PP_ERROR_NOINTERFACE);
223 } // namespace pp