2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2008 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkPixelRef_DEFINED
|
|
|
|
#define SkPixelRef_DEFINED
|
|
|
|
|
2011-12-02 19:11:17 +00:00
|
|
|
#include "SkBitmap.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
|
|
|
|
class SkColorTable;
|
2011-04-01 19:05:36 +00:00
|
|
|
struct SkIRect;
|
2008-12-17 15:59:43 +00:00
|
|
|
class SkMutex;
|
|
|
|
class SkFlattenableReadBuffer;
|
|
|
|
class SkFlattenableWriteBuffer;
|
|
|
|
|
2010-09-09 16:01:26 +00:00
|
|
|
// this is an opaque class, not interpreted by skia
|
|
|
|
class SkGpuTexture;
|
|
|
|
|
2011-12-15 14:16:43 +00:00
|
|
|
#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
|
|
|
|
|
|
|
|
#define SK_DECLARE_PIXEL_REF_REGISTRAR()
|
|
|
|
|
|
|
|
#define SK_DEFINE_PIXEL_REF_REGISTRAR(pixelRef) \
|
|
|
|
static SkPixelRef::Registrar g##pixelRef##Reg(#pixelRef, \
|
|
|
|
pixelRef::Create);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define SK_DECLARE_PIXEL_REF_REGISTRAR() static void Init();
|
|
|
|
|
|
|
|
#define SK_DEFINE_PIXEL_REF_REGISTRAR(pixelRef) \
|
|
|
|
void pixelRef::Init() { \
|
|
|
|
SkPixelRef::Registrar(#pixelRef, Create); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \class SkPixelRef
|
|
|
|
|
|
|
|
This class is the smart container for pixel memory, and is used with
|
|
|
|
SkBitmap. A pixelref is installed into a bitmap, and then the bitmap can
|
|
|
|
access the actual pixel memory by calling lockPixels/unlockPixels.
|
|
|
|
|
|
|
|
This class can be shared/accessed between multiple threads.
|
|
|
|
*/
|
2012-02-09 16:07:08 +00:00
|
|
|
class SK_API SkPixelRef : public SkRefCnt {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-01-26 21:26:40 +00:00
|
|
|
explicit SkPixelRef(SkBaseMutex* mutex = NULL);
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the pixel memory returned from lockPixels, or null if the
|
|
|
|
lockCount is 0.
|
|
|
|
*/
|
|
|
|
void* pixels() const { return fPixels; }
|
|
|
|
|
|
|
|
/** Return the current colorTable (if any) if pixels are locked, or null.
|
|
|
|
*/
|
|
|
|
SkColorTable* colorTable() const { return fColorTable; }
|
|
|
|
|
|
|
|
/** Return the current lockcount (defaults to 0)
|
|
|
|
*/
|
|
|
|
int getLockCount() const { return fLockCount; }
|
|
|
|
|
|
|
|
/** Call to access the pixel memory, which is returned. Balance with a call
|
|
|
|
to unlockPixels().
|
|
|
|
*/
|
|
|
|
void lockPixels();
|
|
|
|
/** Call to balanace a previous call to lockPixels(). Returns the pixels
|
|
|
|
(or null) after the unlock. NOTE: lock calls can be nested, but the
|
|
|
|
matching number of unlock calls must be made in order to free the
|
|
|
|
memory (if the subclass implements caching/deferred-decoding.)
|
|
|
|
*/
|
|
|
|
void unlockPixels();
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2011-07-07 13:42:37 +00:00
|
|
|
/**
|
|
|
|
* Some bitmaps can return a copy of their pixels for lockPixels(), but
|
|
|
|
* that copy, if modified, will not be pushed back. These bitmaps should
|
|
|
|
* not be used as targets for a raster device/canvas (since all pixels
|
|
|
|
* modifications will be lost when unlockPixels() is called.)
|
|
|
|
*/
|
|
|
|
bool lockPixelsAreWritable() const;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns a non-zero, unique value corresponding to the pixels in this
|
|
|
|
pixelref. Each time the pixels are changed (and notifyPixelsChanged is
|
|
|
|
called), a different generation ID will be returned.
|
|
|
|
*/
|
|
|
|
uint32_t getGenerationID() const;
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Call this if you have changed the contents of the pixels. This will in-
|
|
|
|
turn cause a different generation ID value to be returned from
|
|
|
|
getGenerationID().
|
|
|
|
*/
|
|
|
|
void notifyPixelsChanged();
|
|
|
|
|
|
|
|
/** Returns true if this pixelref is marked as immutable, meaning that the
|
|
|
|
contents of its pixels will not change for the lifetime of the pixelref.
|
|
|
|
*/
|
|
|
|
bool isImmutable() const { return fIsImmutable; }
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Marks this pixelref is immutable, meaning that the contents of its
|
|
|
|
pixels will not change for the lifetime of the pixelref. This state can
|
|
|
|
be set on a pixelref, but it cannot be cleared once it is set.
|
|
|
|
*/
|
|
|
|
void setImmutable();
|
|
|
|
|
|
|
|
/** Return the optional URI string associated with this pixelref. May be
|
|
|
|
null.
|
|
|
|
*/
|
|
|
|
const char* getURI() const { return fURI.size() ? fURI.c_str() : NULL; }
|
|
|
|
|
|
|
|
/** Copy a URI string to this pixelref, or clear the URI if the uri is null
|
|
|
|
*/
|
|
|
|
void setURI(const char uri[]) {
|
|
|
|
fURI.set(uri);
|
|
|
|
}
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Copy a URI string to this pixelref
|
|
|
|
*/
|
|
|
|
void setURI(const char uri[], size_t len) {
|
|
|
|
fURI.set(uri, len);
|
|
|
|
}
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Assign a URI string to this pixelref.
|
|
|
|
*/
|
|
|
|
void setURI(const SkString& uri) { fURI = uri; }
|
|
|
|
|
2010-09-09 16:01:26 +00:00
|
|
|
/** Are we really wrapping a texture instead of a bitmap?
|
|
|
|
*/
|
|
|
|
virtual SkGpuTexture* getTexture() { return NULL; }
|
|
|
|
|
2011-04-01 19:05:36 +00:00
|
|
|
bool readPixels(SkBitmap* dst, const SkIRect* subset = NULL);
|
|
|
|
|
2011-12-02 19:11:17 +00:00
|
|
|
/** Makes a deep copy of this PixelRef, respecting the requested config.
|
|
|
|
Returns NULL if either there is an error (e.g. the destination could
|
|
|
|
not be created with the given config), or this PixelRef does not
|
|
|
|
support deep copies. */
|
|
|
|
virtual SkPixelRef* deepCopy(SkBitmap::Config config) { return NULL; }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// serialization
|
|
|
|
|
|
|
|
typedef SkPixelRef* (*Factory)(SkFlattenableReadBuffer&);
|
|
|
|
|
|
|
|
virtual Factory getFactory() const { return NULL; }
|
|
|
|
virtual void flatten(SkFlattenableWriteBuffer&) const;
|
|
|
|
|
2011-11-08 19:00:26 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
2011-02-24 18:09:46 +00:00
|
|
|
/**
|
|
|
|
* Acquire a "global" ref on this object.
|
|
|
|
* The default implementation just calls ref(), but subclasses can override
|
|
|
|
* this method to implement additional behavior.
|
|
|
|
*/
|
2011-02-23 20:46:31 +00:00
|
|
|
virtual void globalRef(void* data=NULL);
|
|
|
|
|
2011-02-24 18:09:46 +00:00
|
|
|
/**
|
|
|
|
* Release a "global" ref on this object.
|
|
|
|
* The default implementation just calls unref(), but subclasses can override
|
|
|
|
* this method to implement additional behavior.
|
|
|
|
*/
|
2011-02-23 20:46:31 +00:00
|
|
|
virtual void globalUnref();
|
2011-02-24 18:09:46 +00:00
|
|
|
#endif
|
2011-02-23 20:46:31 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static Factory NameToFactory(const char name[]);
|
|
|
|
static const char* FactoryToName(Factory);
|
|
|
|
static void Register(const char name[], Factory);
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
class Registrar {
|
|
|
|
public:
|
|
|
|
Registrar(const char name[], Factory factory) {
|
|
|
|
SkPixelRef::Register(name, factory);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** Called when the lockCount goes from 0 to 1. The caller will have already
|
|
|
|
acquire a mutex for thread safety, so this method need not do that.
|
|
|
|
*/
|
|
|
|
virtual void* onLockPixels(SkColorTable**) = 0;
|
|
|
|
/** Called when the lock count goes from 1 to 0. The caller will have
|
|
|
|
already acquire a mutex for thread safety, so this method need not do
|
|
|
|
that.
|
|
|
|
*/
|
|
|
|
virtual void onUnlockPixels() = 0;
|
|
|
|
|
2011-07-07 13:42:37 +00:00
|
|
|
/** Default impl returns true */
|
|
|
|
virtual bool onLockPixelsAreWritable() const;
|
|
|
|
|
2011-04-01 19:05:36 +00:00
|
|
|
/**
|
|
|
|
* For pixelrefs that don't have access to their raw pixels, they may be
|
|
|
|
* able to make a copy of them (e.g. if the pixels are on the GPU).
|
|
|
|
*
|
|
|
|
* The base class implementation returns false;
|
|
|
|
*/
|
|
|
|
virtual bool onReadPixels(SkBitmap* dst, const SkIRect* subsetOrNull);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the mutex associated with this pixelref. This value is assigned
|
|
|
|
in the constructor, and cannot change during the lifetime of the object.
|
|
|
|
*/
|
2012-01-26 21:26:40 +00:00
|
|
|
SkBaseMutex* mutex() const { return fMutex; }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-01-26 21:26:40 +00:00
|
|
|
SkPixelRef(SkFlattenableReadBuffer&, SkBaseMutex*);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
private:
|
2011-12-15 14:16:43 +00:00
|
|
|
#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
|
|
|
|
static void InitializeFlattenables();
|
|
|
|
#endif
|
|
|
|
|
2012-01-26 21:26:40 +00:00
|
|
|
SkBaseMutex* fMutex; // must remain in scope for the life of this object
|
2008-12-17 15:59:43 +00:00
|
|
|
void* fPixels;
|
|
|
|
SkColorTable* fColorTable; // we do not track ownership, subclass does
|
|
|
|
int fLockCount;
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
mutable uint32_t fGenerationID;
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkString fURI;
|
|
|
|
|
|
|
|
// can go from false to true, but never from true to false
|
|
|
|
bool fIsImmutable;
|
2011-12-20 20:26:56 +00:00
|
|
|
|
|
|
|
friend class SkGraphics;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|