blob: 3f2b53d369bdcc263b5510028e0683f0ae69be51 [file] [log] [blame]
// Copyright 2015 The Cobalt Authors. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include <vector>
#include "cobalt/loader/image/image.h"
#include "cobalt/render_tree/resource_provider.h"
#if defined(STARBOARD)
#include "starboard/decode_target.h"
namespace cobalt {
namespace loader {
namespace image {
// This class is used as a component of ImageDecoder and is intended to be
// sub-classed by specific data decoders, such as JPEG decoders and PNG
// decoders. This class is intended to decode image data based on the type,
// but leaves the responsibility of handling callbacks and producing the final
// image to ImageDecoder.
class ImageDataDecoder {
explicit ImageDataDecoder(render_tree::ResourceProvider* resource_provider);
virtual ~ImageDataDecoder() {}
virtual std::string GetTypeString() const = 0;
scoped_ptr<render_tree::ImageData> RetrieveImageData() {
return image_data_.Pass();
#if defined(STARBOARD)
// Starboard version 3 adds support for hardware accelerated image decoding.
// In order to make use of this feature, subclasses of ImageDataDecoder may
// override this method in order to return an SbDecodeTarget, rather than a
// render_tree::ImageData, which could potentially save a copy from CPU
// memory to GPU memory, depending on the render_tree implementation and
// hardware image decoding functionality available. If
// |RetrieveSbDecodeTarget| returns any value other than the default
// |kSbDecodeInvalid|, ImageDecoder will interpret it as a preference
// towards SbDecodeTarget rather than render_tree::ImageData.
virtual SbDecodeTarget RetrieveSbDecodeTarget() {
return kSbDecodeTargetInvalid;
#endif // SB_HAS(GRAPHICS)
#endif // defined(STARBOARD)
void DecodeChunk(const uint8* data, size_t size);
// Return true if decoding succeeded.
bool FinishWithSuccess();
// If a format supports animation, subclass should implement these methods.
// When has_animation() is true, the intermediate result is returned in
// animated_image().
virtual bool has_animation() const { return false; }
virtual scoped_refptr<AnimatedImage> animated_image() { return NULL; }
enum State {
// Every subclass of ImageDataDecoder should override this function to do
// the internal decoding. The return value of this function is the number of
// decoded data.
virtual size_t DecodeChunkInternal(const uint8* data, size_t input_byte) = 0;
// Subclass can override this function to get a last chance to do some work.
virtual void FinishInternal() {}
bool AllocateImageData(const math::Size& size, bool has_alpha);
render_tree::ResourceProvider* resource_provider() {
return resource_provider_;
render_tree::ImageData* image_data() const { return image_data_.get(); }
void set_state(State state) { state_ = state; }
State state() const { return state_; }
render_tree::PixelFormat pixel_format() const { return pixel_format_; }
// Called on construction to query the ResourceProvider for the best image
// pixel format to use.
void CalculatePixelFormat();
// |resource_provider_| is used to allocate render_tree::ImageData
render_tree::ResourceProvider* const resource_provider_;
// Decoded image data.
scoped_ptr<render_tree::ImageData> image_data_;
// |data_buffer_| is used to cache the undecoded data.
std::vector<uint8> data_buffer_;
// Record the current decoding status.
State state_;
// The pixel format that the decoded image data is expected to be in.
render_tree::PixelFormat pixel_format_;
} // namespace image
} // namespace loader
} // namespace cobalt