SkImage.h revision da90474b5fcc019fb0971d12360bd05213ad4dc8
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 "SkRefCnt.h"
12#include "SkScalar.h"
13
14class SkData;
15class SkCanvas;
16class SkPaint;
17class SkShader;
18class GrContext;
19class GrTexture;
20
21// need for TileMode
22#include "SkShader.h"
23
24////// EXPERIMENTAL
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        kPMColor_ColorType,
46
47        kLastEnum_ColorType = kPMColor_ColorType
48    };
49
50    enum AlphaType {
51        kIgnore_AlphaType,
52        kOpaque_AlphaType,
53        kPremul_AlphaType,
54        kUnpremul_AlphaType,
55
56        kLastEnum_AlphaType = kUnpremul_AlphaType
57    };
58
59    struct Info {
60        int         fWidth;
61        int         fHeight;
62        ColorType   fColorType;
63        AlphaType   fAlphaType;
64    };
65
66    static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes);
67    static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes);
68    static SkImage* NewEncodedData(SkData*);
69    static SkImage* NewTexture(GrTexture*);
70
71    int width() const { return fWidth; }
72    int height() const { return fHeight; }
73    uint32_t uniqueID() const { return fUniqueID; }
74
75    /**
76     * Return the GrTexture that stores the image pixels. Calling getTexture
77     * does not affect the reference count of the GrTexture object.
78     * Will return NULL if the image does not use a texture.
79     */
80    GrTexture* getTexture();
81
82    SkShader*   newShaderClamp() const;
83    SkShader*   newShader(SkShader::TileMode, SkShader::TileMode) const;
84
85    void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
86
87protected:
88    SkImage(int width, int height) :
89        fWidth(width),
90        fHeight(height),
91        fUniqueID(NextUniqueID()) {
92
93        SkASSERT(width >= 0);
94        SkASSERT(height >= 0);
95    }
96
97private:
98    const int       fWidth;
99    const int       fHeight;
100    const uint32_t  fUniqueID;
101
102    static uint32_t NextUniqueID();
103
104    typedef SkRefCnt INHERITED;
105};
106
107#endif
108