SkGpuDevice.h revision 99e5b524ced6d299f129bf572960b87579646bb4
1
2/*
3 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10
11#ifndef SkGpuDevice_DEFINED
12#define SkGpuDevice_DEFINED
13
14#include "SkGr.h"
15#include "SkBitmap.h"
16#include "SkBitmapDevice.h"
17#include "SkPicture.h"
18#include "SkRegion.h"
19#include "GrContext.h"
20
21struct SkDrawProcs;
22struct GrSkDrawProcs;
23
24class GrTextContext;
25
26/**
27 *  Subclass of SkBitmapDevice, which directs all drawing to the GrGpu owned by the
28 *  canvas.
29 */
30class SK_API SkGpuDevice : public SkBitmapDevice {
31public:
32    enum Flags {
33        kNeedClear_Flag = 1 << 0,  //!< Surface requires an initial clear
34        kCached_Flag    = 1 << 1,  //!< Surface is cached and needs to be unlocked when released
35    };
36
37    /**
38     * Creates an SkGpuDevice from a GrSurface. This will fail if the surface is not a render
39     * target. The caller owns a ref on the returned device. If the surface is cached,
40     * the kCached_Flag should be specified to make the device responsible for unlocking
41     * the surface when it is released.
42     */
43    static SkGpuDevice* Create(GrSurface* surface, unsigned flags = 0);
44
45    /**
46     *  New device that will create an offscreen renderTarget based on the
47     *  ImageInfo and sampleCount. The device's storage will not
48     *  count against the GrContext's texture cache budget. The device's pixels
49     *  will be uninitialized. On failure, returns NULL.
50     */
51    static SkGpuDevice* Create(GrContext*, const SkImageInfo&, int sampleCount);
52
53#ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG
54    /**
55     *  New device that will create an offscreen renderTarget based on the
56     *  config, width, height, and sampleCount. The device's storage will not
57     *  count against the GrContext's texture cache budget. The device's pixels
58     *  will be uninitialized. TODO: This can fail, replace with a factory function.
59     */
60    SkGpuDevice(GrContext*, SkBitmap::Config, int width, int height, int sampleCount = 0);
61#endif
62
63    /**
64     *  DEPRECATED -- need to make this private, call Create(surface)
65     *  New device that will render to the specified renderTarget.
66     */
67    SkGpuDevice(GrContext*, GrRenderTarget*, unsigned flags = 0);
68
69    /**
70     *  DEPRECATED -- need to make this private, call Create(surface)
71     *  New device that will render to the texture (as a rendertarget).
72     *  The GrTexture's asRenderTarget() must be non-NULL or device will not
73     *  function.
74     */
75    SkGpuDevice(GrContext*, GrTexture*, unsigned flags = 0);
76
77    virtual ~SkGpuDevice();
78
79    GrContext* context() const { return fContext; }
80
81    virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
82
83    // overrides from SkBaseDevice
84    virtual int width() const SK_OVERRIDE {
85        return NULL == fRenderTarget ? 0 : fRenderTarget->width();
86    }
87    virtual int height() const SK_OVERRIDE {
88        return NULL == fRenderTarget ? 0 : fRenderTarget->height();
89    }
90    virtual bool isOpaque() const SK_OVERRIDE {
91        return NULL == fRenderTarget ? false
92                                     : kRGB_565_GrPixelConfig == fRenderTarget->config();
93    }
94    virtual SkBitmap::Config config() const SK_OVERRIDE;
95
96    virtual void clear(SkColor color) SK_OVERRIDE;
97    virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
98    virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
99                            const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
100    virtual void drawRect(const SkDraw&, const SkRect& r,
101                          const SkPaint& paint) SK_OVERRIDE;
102    virtual void drawRRect(const SkDraw&, const SkRRect& r,
103                           const SkPaint& paint) SK_OVERRIDE;
104    virtual void drawOval(const SkDraw&, const SkRect& oval,
105                          const SkPaint& paint) SK_OVERRIDE;
106    virtual void drawPath(const SkDraw&, const SkPath& path,
107                          const SkPaint& paint, const SkMatrix* prePathMatrix,
108                          bool pathIsMutable) SK_OVERRIDE;
109    virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
110                            const SkMatrix&, const SkPaint&) SK_OVERRIDE;
111    virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
112                                const SkRect* srcOrNull, const SkRect& dst,
113                                const SkPaint& paint,
114                                SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
115    virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
116                            int x, int y, const SkPaint& paint);
117    virtual void drawText(const SkDraw&, const void* text, size_t len,
118                          SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
119    virtual void drawPosText(const SkDraw&, const void* text, size_t len,
120                             const SkScalar pos[], SkScalar constY,
121                             int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
122    virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
123                                const SkPath& path, const SkMatrix* matrix,
124                                const SkPaint&) SK_OVERRIDE;
125    virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
126                              const SkPoint verts[], const SkPoint texs[],
127                              const SkColor colors[], SkXfermode* xmode,
128                              const uint16_t indices[], int indexCount,
129                              const SkPaint&) SK_OVERRIDE;
130    virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
131                            const SkPaint&) SK_OVERRIDE;
132    virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE;
133
134    virtual void flush() SK_OVERRIDE;
135
136    virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
137    virtual void onDetachFromCanvas() SK_OVERRIDE;
138
139    virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
140
141    /**
142     * Make's this device's rendertarget current in the underlying 3D API.
143     * Also implicitly flushes.
144     */
145    virtual void makeRenderTargetCurrent();
146
147    virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE;
148    virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
149                             const SkImageFilter::Context&,
150                             SkBitmap*, SkIPoint*) SK_OVERRIDE;
151
152    class SkAutoCachedTexture; // used internally
153
154
155protected:
156#ifdef SK_SUPPORT_LEGACY_READPIXELSCONFIG
157    virtual bool onReadPixels(const SkBitmap&, int x, int y, SkCanvas::Config8888) SK_OVERRIDE;
158#endif
159    virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int, int) SK_OVERRIDE;
160    virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) SK_OVERRIDE;
161
162    /**  PRIVATE / EXPERIMENTAL -- do not call */
163    virtual void EXPERIMENTAL_optimize(SkPicture* picture) SK_OVERRIDE;
164    /**  PRIVATE / EXPERIMENTAL -- do not call */
165    virtual bool EXPERIMENTAL_drawPicture(const SkPicture& picture) SK_OVERRIDE;
166
167private:
168    GrContext*      fContext;
169
170    GrSkDrawProcs*  fDrawProcs;
171
172    GrClipData      fClipData;
173
174    GrTextContext*  fMainTextContext;
175    GrTextContext*  fFallbackTextContext;
176
177    // state for our render-target
178    GrRenderTarget*     fRenderTarget;
179    bool                fNeedClear;
180
181    // called from rt and tex cons
182    void initFromRenderTarget(GrContext*, GrRenderTarget*, unsigned flags);
183
184    virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
185
186    virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
187
188    // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override
189    // SkDraw's matrix and draw in device coords.
190    void prepareDraw(const SkDraw&, bool forceIdentity);
191
192    /**
193     * Implementation for both drawBitmap and drawBitmapRect.
194     */
195    void drawBitmapCommon(const SkDraw&,
196                          const SkBitmap& bitmap,
197                          const SkRect* srcRectPtr,
198                          const SkSize* dstSizePtr,      // ignored iff srcRectPtr == NULL
199                          const SkPaint&,
200                          SkCanvas::DrawBitmapRectFlags flags);
201
202    /**
203     * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
204     * matrix, clip, and the device's render target has already been set on GrContext.
205     */
206
207    // The tileSize and clippedSrcRect will be valid only if true is returned.
208    bool shouldTileBitmap(const SkBitmap& bitmap,
209                          const GrTextureParams& sampler,
210                          const SkRect* srcRectPtr,
211                          int maxTileSize,
212                          int* tileSize,
213                          SkIRect* clippedSrcRect) const;
214    void internalDrawBitmap(const SkBitmap&,
215                            const SkRect&,
216                            const GrTextureParams& params,
217                            const SkPaint& paint,
218                            SkCanvas::DrawBitmapRectFlags flags,
219                            bool bicubic);
220    void drawTiledBitmap(const SkBitmap& bitmap,
221                         const SkRect& srcRect,
222                         const SkIRect& clippedSrcRect,
223                         const GrTextureParams& params,
224                         const SkPaint& paint,
225                         SkCanvas::DrawBitmapRectFlags flags,
226                         int tileSize,
227                         bool bicubic);
228
229    static SkPicture::AccelData::Key ComputeAccelDataKey();
230
231    typedef SkBitmapDevice INHERITED;
232};
233
234#endif
235