diff options
author | gman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-11 18:12:50 +0000 |
---|---|---|
committer | gman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-11 18:12:50 +0000 |
commit | ed97932b6ec58d01baea6948d5506503247f2986 (patch) | |
tree | 19ae1cfc4de8ce29ba75ab0386d1f56a1bacf57a /o3d/core/cross/bitmap.h | |
parent | f963b1d5006ed35450befe042bf58810d9d36bea (diff) | |
download | chromium_src-ed97932b6ec58d01baea6948d5506503247f2986.zip chromium_src-ed97932b6ec58d01baea6948d5506503247f2986.tar.gz chromium_src-ed97932b6ec58d01baea6948d5506503247f2986.tar.bz2 |
Changes to Bitmap before exposing to JavaScript
This CL changes Bitmap to no longer be a cubemap.
Instead there is a function, Pack::CreateBitmapsFromRawData
that returns an array of bitmaps.
For a 2D image 1 bitmap is returned
For a cubemap 6 bitmaps are returned
eventually for a volumemap N bitmaps will be returned.
To distinguish between 6 bitmaps that are a cubemap
and 6 bitmaps that are slices of a volumemap there
is a Bitmap::semantic which tells what the bitmap is
intended for.
In a previous CL I had started to break Bitmap into
classes like Bitmap8, BitmapFloat, BitmapDXT1.
These were not intended to be exposed to JavaScript,
only to make the internal code get rid of lots of
if format == or case(format) stuff.
But given that it's working as is and there are more
pressing things I'm not planning to do that right now.
I can delete the classes if you think I should.
Note: This is still not 100% done. I still need to
deal with the flipping issues or at least test.
I probably need to add more tests as well.
I also got rid of any mention of resize_to_pot in
the command buffer version. Client side command
buffer should not have to deal with POT/NPOT issues.
I also moved the pot/npot stuff out of generic code
and into the platform specific code. The generic code
is not supposed to care.
This is slower than the old way but it feels cleaner.
A few issues I'm looking for input on. There's
a function, Bitmap::GenerateMips(source_level, num_levels).
It generates mips as long as they 8Bit textures.
I can easily add half and float but not DXT. Right now
if you call it on DXT in debug it prints a LOG message
but otherwise it does nothing. Should I error for DXT?
On the one hand it would be good to know that it failed
so a user will not be wondering "I called it, why are
there no mips?"
On the otherhand, from JavaScript failing would mean
you'd need to check the format before calling it which
also seems less then ideal.
The other is that currently it doesn't ADD mips, it just
replaces the ones that are there. That means if you load
a 2d image and want mips you have to allocate a new bitmap
with the number of mips you want, copy level 0 from the
old bitmap to the new bitmap and then generate mips
Should I make generate mips effectively do that for you?
Basically, if you call it to generate mips on levels
that don't yet exist in the bitmap it would realloc itself
and then generate them. Is that better?
Also, I started down the path of taking out alpha_is_one.
I'm trying to decide what to do with it. Part of me wants
to take it out completely but since it's already there
maybe I can't.
If I leave it in I was thinking of making it check every
time it's called. First I'd just make it slow. Then later
if we want I could add a dirty flag and only recheck when
it's dirty. That would be a lot of work though. It would
mean wrapping Lock and SetRect and maybe a few other things
so that we could catch any writes to the texture. It might
also mean changing Lock to take read/write flags. I think
those are good changes but again, it's not important right
now. So, the question is what to do now. I can
1) Remove alpha_is_one. That might break something out there
2) Leave it in but just have it always false.
3) Make it check every time it's accessed.
4) Do the dirty flag thing.
Preference? I'm for 2 or 3 I think.
Review URL: http://codereview.chromium.org/164235
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23051 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d/core/cross/bitmap.h')
-rw-r--r-- | o3d/core/cross/bitmap.h | 189 |
1 files changed, 85 insertions, 104 deletions
diff --git a/o3d/core/cross/bitmap.h b/o3d/core/cross/bitmap.h index a86b76a..a116ecb 100644 --- a/o3d/core/cross/bitmap.h +++ b/o3d/core/cross/bitmap.h @@ -41,7 +41,7 @@ #define O3D_CORE_CROSS_BITMAP_H_
#include <stdlib.h>
-
+#include <vector>
#include "base/cross/bits.h"
#include "core/cross/types.h"
#include "core/cross/texture.h"
@@ -56,27 +56,27 @@ class RawData; class Pack;
// Bitmap provides an API for basic image operations on bitmap images,
-// including scale and crop. The contents of bitmap can be created from
-// a RawData object via LoadFromRawData(), and also can be transfered
-// to mip of a Texure2D or a specific face of TextureCUBE via methods
-// in Texture.
+// including scale and crop. Bitmaps can be created from a RawData object via
+// LoadFromRawData(), and also can be transfered to mips of a Texure2D or a
+// specific face of TextureCUBE via methods in Texture.
class Bitmap : public ParamObject {
public:
typedef SmartPointer<Bitmap> Ref;
+ typedef std::vector<Bitmap::Ref> BitmapRefArray;
explicit Bitmap(ServiceLocator* service_locator);
virtual ~Bitmap() {}
enum Semantic {
- FACE_POSITIVE_X,
+ FACE_POSITIVE_X, // NOTE: These must match TextureCUBE::CubeFace
FACE_NEGATIVE_X,
FACE_POSITIVE_Y,
FACE_NEGATIVE_Y,
FACE_POSITIVE_Z,
FACE_NEGATIVE_Z,
- NORMAL,
- SLICE,
+ IMAGE, // normal 2d image
+ SLICE, // a slice of a 3d texture.
};
// Returns the pitch of the bitmap for a certain level.
@@ -89,7 +89,7 @@ class Bitmap : public ParamObject { // source: the source bitmap.
void CopyDeepFrom(const Bitmap &source) {
Allocate(source.format_, source.width_, source.height_,
- source.num_mipmaps_, source.is_cubemap_);
+ source.num_mipmaps_, source.semantic_);
memcpy(image_data(), source.image_data(), GetTotalSize());
}
@@ -97,15 +97,7 @@ class Bitmap : public ParamObject { // from the source bitmap.
// Parameters:
// source: the source bitmap.
- void SetFrom(Bitmap *source) {
- image_data_.reset();
- format_ = source->format_;
- width_ = source->width_;
- height_ = source->height_;
- num_mipmaps_ = source->num_mipmaps_;
- is_cubemap_ = source->is_cubemap_;
- image_data_.swap(source->image_data_);
- }
+ void SetFrom(Bitmap *source);
// Allocates an uninitialized bitmap with specified parameters.
// Parameters:
@@ -113,12 +105,11 @@ class Bitmap : public ParamObject { // width: the width of the base image.
// height: the height of the base image.
// num_mipmaps: the number of mip-maps.
- // cube_map: true if creating a cube map.
void Allocate(Texture::Format format,
unsigned int width,
unsigned int height,
unsigned int num_mipmaps,
- bool cube_map);
+ Semantic semantic);
// Allocates a bitmap with initialized parameters.
// data is zero-initialized
@@ -153,31 +144,9 @@ class Bitmap : public ParamObject { const void* src_data,
int src_pitch);
- // Sets a rectangular region of this bitmap.
- // If the bitmap is a DXT format, the only acceptable values
- // for left, top, width and height are 0, 0, bitmap->width, bitmap->height
- //
- // Parameters:
- // level: The mipmap level to modify
- // dst_left: The left edge of the rectangular area to modify.
- // dst_top: The top edge of the rectangular area to modify.
- // width: The width of the rectangular area to modify.
- // height: The of the rectangular area to modify.
- // src_data: The source pixels.
- // src_pitch: If the format is uncompressed this is the number of bytes
- // per row of pixels. If compressed this value is unused.
- void SetFaceRect(TextureCUBE::CubeFace face,
- int level,
- unsigned dst_left,
- unsigned dst_top,
- unsigned width,
- unsigned height,
- const void* src_data,
- int src_pitch);
-
// Gets the total size of the bitmap data, counting all faces and mip levels.
- unsigned int GetTotalSize() const {
- return (is_cubemap_ ? 6 : 1) * GetMipChainSize(num_mipmaps_);
+ size_t GetTotalSize() const {
+ return GetMipChainSize(num_mipmaps_);
}
// Gets the image data for a given mip-map level.
@@ -185,44 +154,34 @@ class Bitmap : public ParamObject { // level: mip level to get.
uint8 *GetMipData(unsigned int level) const;
- // Gets the image data for a given mip-map level and cube map face.
- // Parameters:
- // face: face of cube to get. This parameter is ignored if
- // this bitmap is not a cube map.
- // level: mip level to get.
- uint8 *GetFaceMipData(TextureCUBE::CubeFace face, unsigned int level) const;
-
// Gets the size of mip.
- unsigned int GetMipSize(unsigned int level) const;
+ size_t GetMipSize(unsigned int level) const;
uint8 *image_data() const { return image_data_.get(); }
Texture::Format format() const { return format_; }
unsigned int width() const { return width_; }
unsigned int height() const { return height_; }
unsigned int num_mipmaps() const { return num_mipmaps_; }
- bool is_cubemap() const { return is_cubemap_; }
+ Semantic semantic() const {
+ return semantic_;
+ }
// Returns whether or not the dimensions of the bitmap are power-of-two.
bool IsPOT() const {
- return ((width_ & (width_ - 1)) == 0) && ((height_ & (height_ - 1)) == 0);
+ return image::IsPOT(width_, height_);
}
- void set_format(Texture::Format format) { format_ = format; }
- void set_width(unsigned int n) { width_ = n; }
- void set_height(unsigned int n) { height_ = n; }
- void set_num_mipmaps(unsigned int n) { num_mipmaps_ = n; }
- void set_is_cubemap(bool is_cubemap) { is_cubemap_ = is_cubemap; }
-
// Loads a bitmap from a file.
// Parameters:
// filename: the name of the file to load.
// file_type: the type of file to load. If UNKNOWN, the file type will be
// determined from the filename extension, and if it is not a
// known extension, all the loaders will be tried.
- // generate_mipmaps: whether or not to generate all the mip-map levels.
- bool LoadFromFile(const FilePath &filepath,
- image::ImageFileType file_type,
- bool generate_mipmaps);
+ // bitmaps: An array to hold references to the loaded bitmaps.
+ static bool LoadFromFile(ServiceLocator* service_locator,
+ const FilePath &filepath,
+ image::ImageFileType file_type,
+ BitmapRefArray* bitmaps);
// Loads a bitmap from a RawData object.
// Parameters:
@@ -231,10 +190,10 @@ class Bitmap : public ParamObject { // will be determined from the extension from raw_data's uri
// and if it is not a known extension, all the loaders will
// be tried.
- // generate_mipmaps: whether or not to generate all the mip-map levels.
- bool LoadFromRawData(RawData *raw_data,
- image::ImageFileType file_type,
- bool generate_mipmaps);
+ // bitmaps: An array to hold references to the loaded bitmaps.
+ static bool LoadFromRawData(RawData *raw_data,
+ image::ImageFileType file_type,
+ BitmapRefArray* bitmaps);
// Flips a bitmap vertically in place.
// This is needed instead of just using DrawImage because flipping DXT formats
@@ -269,7 +228,7 @@ class Bitmap : public ParamObject { // Gets the size of the buffer containing a mip-map chain, given a number of
// mip-map levels.
- unsigned int GetMipChainSize(unsigned int num_mipmaps) const {
+ size_t GetMipChainSize(unsigned int num_mipmaps) const {
return image::ComputeMipChainSize(width(), height(), format(), num_mipmaps);
}
@@ -280,20 +239,22 @@ class Bitmap : public ParamObject { friend class IClassManager;
static ObjectBase::Ref Create(ServiceLocator* service_locator);
- // pointer to the raw bitmap data
- scoped_array<uint8> image_data_;
- // format of the texture this is meant to represent.
- Texture::Format format_;
- // width of the bitmap in pixels.
- int width_;
- // height of the bitmap in pixels.
- int height_;
- // number of mipmap levels in this texture.
- unsigned int num_mipmaps_;
- // is this cube-map data
- bool is_cubemap_;
-
- // Loads a bitmap from a MemoryReadStream.
+ // Sets the contents of a Bitmap replacing any previous contents.
+ // Parameters:
+ // format: Format of the bitmap.
+ // num_mipmaps: The number of mipmaps.
+ // width: width in pixels.
+ // height: height in pixels.
+ // semantic: the semantic of the bitmap
+ // image_data: The image data. The bitmap will take ownership of this data.
+ void SetContents(Texture::Format format,
+ unsigned int num_mipmaps,
+ unsigned int width,
+ unsigned int height,
+ Semantic semantic,
+ scoped_array<uint8>* image_data);
+
+ // Loads bitmaps from a MemoryReadStream.
// Parameters:
// stream: a stream for the bitmap data in one of the known formats
// filename: a filename (or uri) of the original bitmap data
@@ -302,27 +263,32 @@ class Bitmap : public ParamObject { // will be determined from the extension of |filename|
// and if it is not a known extension, all the loaders
// will be tried.
- // generate_mipmaps: whether or not to generate all the mip-map levels.
- bool LoadFromStream(MemoryReadStream *stream,
- const String &filename,
- image::ImageFileType file_type,
- bool generate_mipmaps);
-
- bool LoadFromPNGStream(MemoryReadStream *stream,
- const String &filename,
- bool generate_mipmaps);
-
- bool LoadFromTGAStream(MemoryReadStream *stream,
- const String &filename,
- bool generate_mipmaps);
-
- bool LoadFromDDSStream(MemoryReadStream *stream,
- const String &filename,
- bool generate_mipmaps);
-
- bool LoadFromJPEGStream(MemoryReadStream *stream,
- const String &filename,
- bool generate_mipmaps);
+ // bitmaps: An array to hold references to the loaded bitmaps.
+ static bool LoadFromStream(ServiceLocator* service_locator,
+ MemoryReadStream *stream,
+ const String &filename,
+ image::ImageFileType file_type,
+ BitmapRefArray* bitmaps);
+
+ static bool LoadFromPNGStream(ServiceLocator* service_locator,
+ MemoryReadStream *stream,
+ const String &filename,
+ BitmapRefArray* bitmaps);
+
+ static bool LoadFromTGAStream(ServiceLocator* service_locator,
+ MemoryReadStream *stream,
+ const String &filename,
+ BitmapRefArray* bitmaps);
+
+ static bool LoadFromDDSStream(ServiceLocator* service_locator,
+ MemoryReadStream *stream,
+ const String &filename,
+ BitmapRefArray* bitmaps);
+
+ static bool LoadFromJPEGStream(ServiceLocator* service_locator,
+ MemoryReadStream *stream,
+ const String &filename,
+ BitmapRefArray* bitmaps);
bool GenerateMipmaps(unsigned int base_width,
unsigned int base_height,
@@ -330,10 +296,25 @@ class Bitmap : public ParamObject { unsigned int num_mipmaps,
uint8 *data);
+ // pointer to the raw bitmap data
+ scoped_array<uint8> image_data_;
+ // format of the texture this is meant to represent.
+ Texture::Format format_;
+ // width of the bitmap in pixels.
+ int width_;
+ // height of the bitmap in pixels.
+ int height_;
+ // number of mipmap levels in this texture.
+ unsigned int num_mipmaps_;
+ // The purpose of the bitmap
+ Semantic semantic_;
+
O3D_DECL_CLASS(Bitmap, ParamObject);
DISALLOW_COPY_AND_ASSIGN(Bitmap);
};
+typedef Bitmap::BitmapRefArray BitmapRefArray;
+
class BitmapUncompressed : public Bitmap {
public:
explicit BitmapUncompressed(ServiceLocator* service_locator);
|