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