- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / core / SkImage.h
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #ifndef SkImage_DEFINED
9 #define SkImage_DEFINED
10
11 #include "SkAlpha.h"
12 #include "SkImageEncoder.h"
13 #include "SkRefCnt.h"
14 #include "SkScalar.h"
15
16 class SkData;
17 class SkCanvas;
18 class SkPaint;
19 class SkShader;
20 class GrContext;
21 class GrTexture;
22
23 // need for TileMode
24 #include "SkShader.h"
25
26 enum SkColorType {
27     kAlpha_8_SkColorType,
28     kRGB_565_SkColorType,
29     kRGBA_8888_SkColorType,
30     kBGRA_8888_SkColorType,
31
32 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
33     kPMColor_SkColorType = kBGRA_8888_SkColorType,
34 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
35     kPMColor_SkColorType = kRGBA_8888_SkColorType,
36 #else
37     #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
38 #endif
39
40     kLastEnum_SkColorType = kBGRA_8888_SkColorType
41 };
42
43 struct SkImageInfo {
44     int         fWidth;
45     int         fHeight;
46     SkColorType fColorType;
47     SkAlphaType fAlphaType;
48 };
49
50 /**
51  *  SkImage is an abstraction for drawing a rectagle of pixels, though the
52  *  particular type of image could be actually storing its data on the GPU, or
53  *  as drawing commands (picture or PDF or otherwise), ready to be played back
54  *  into another canvas.
55  *
56  *  The content of SkImage is always immutable, though the actual storage may
57  *  change, if for example that image can be re-created via encoded data or
58  *  other means.
59  */
60 class SK_API SkImage : public SkRefCnt {
61 public:
62     SK_DECLARE_INST_COUNT(SkImage)
63
64 #ifdef SK_SUPPORT_LEGACY_COLORTYPE
65     typedef SkColorType ColorType;
66
67     static const SkColorType kAlpha_8_ColorType     = kAlpha_8_SkColorType;
68     static const SkColorType kRGB_565_ColorType     = kRGB_565_SkColorType;
69     static const SkColorType kRGBA_8888_ColorType   = kRGBA_8888_SkColorType;
70     static const SkColorType kBGRA_8888_ColorType   = kBGRA_8888_SkColorType;
71     static const SkColorType kPMColor_ColorType     = kPMColor_SkColorType;
72     static const SkColorType kLastEnum_ColorType    = kLastEnum_SkColorType;
73 #endif
74
75 #ifdef SK_SUPPORT_LEGACY_ALPHATYPE
76     typedef SkAlphaType AlphaType;
77
78     static const SkAlphaType kIgnore_AlphaType   = kIgnore_SkAlphaType;
79     static const SkAlphaType kOpaque_AlphaType   = kOpaque_SkAlphaType;
80     static const SkAlphaType kPremul_AlphaType   = kPremul_SkAlphaType;
81     static const SkAlphaType kUnpremul_AlphaType = kUnpremul_SkAlphaType;
82 #endif
83
84     typedef SkImageInfo Info;
85
86     static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes);
87     static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes);
88     static SkImage* NewEncodedData(SkData*);
89
90     /**
91      * GrTexture is a more logical parameter for this factory, but its
92      * interactions with scratch cache still has issues, so for now we take
93      * SkBitmap instead. This will be changed in the future. skbug.com/1449
94      */
95     static SkImage* NewTexture(const SkBitmap&);
96
97     int width() const { return fWidth; }
98     int height() const { return fHeight; }
99     uint32_t uniqueID() const { return fUniqueID; }
100
101     /**
102      * Return the GrTexture that stores the image pixels. Calling getTexture
103      * does not affect the reference count of the GrTexture object.
104      * Will return NULL if the image does not use a texture.
105      */
106     GrTexture* getTexture();
107
108     SkShader*   newShaderClamp() const;
109     SkShader*   newShader(SkShader::TileMode, SkShader::TileMode) const;
110
111     void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
112
113     /**
114      *  Draw the image, cropped to the src rect, to the dst rect of a canvas.
115      *  If src is larger than the bounds of the image, the rest of the image is
116      *  filled with transparent black pixels.
117      *
118      *  See SkCanvas::drawBitmapRectToRect for similar behavior.
119      */
120     void draw(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*);
121
122     /**
123      *  Encode the image's pixels and return the result as a new SkData, which
124      *  the caller must manage (i.e. call unref() when they are done).
125      *
126      *  If the image type cannot be encoded, or the requested encoder type is
127      *  not supported, this will return NULL.
128      */
129     SkData* encode(SkImageEncoder::Type t = SkImageEncoder::kPNG_Type,
130                    int quality = 80) const;
131
132 protected:
133     SkImage(int width, int height) :
134         fWidth(width),
135         fHeight(height),
136         fUniqueID(NextUniqueID()) {
137
138         SkASSERT(width >= 0);
139         SkASSERT(height >= 0);
140     }
141
142 private:
143     const int       fWidth;
144     const int       fHeight;
145     const uint32_t  fUniqueID;
146
147     static uint32_t NextUniqueID();
148
149     typedef SkRefCnt INHERITED;
150 };
151
152 #endif