3 * Copyright 2010 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #ifndef SkGpuDevice_DEFINED
12 #define SkGpuDevice_DEFINED
16 #include "SkBitmapDevice.h"
18 #include "GrContext.h"
26 * Subclass of SkBitmapDevice, which directs all drawing to the GrGpu owned by the
29 class SK_API SkGpuDevice : public SkBitmapDevice {
33 * Creates an SkGpuDevice from a GrSurface. This will fail if the surface is not a render
34 * target. The caller owns a ref on the returned device.
36 static SkGpuDevice* Create(GrSurface* surface);
39 * New device that will create an offscreen renderTarget based on the
40 * config, width, height, and sampleCount. The device's storage will not
41 * count against the GrContext's texture cache budget. The device's pixels
42 * will be uninitialized. TODO: This can fail, replace with a factory function.
44 SkGpuDevice(GrContext*, SkBitmap::Config, int width, int height, int sampleCount = 0);
47 * DEPRECATED -- need to make this private, call Create(surface)
48 * New device that will render to the specified renderTarget.
50 SkGpuDevice(GrContext*, GrRenderTarget*);
53 * DEPRECATED -- need to make this private, call Create(surface)
54 * New device that will render to the texture (as a rendertarget).
55 * The GrTexture's asRenderTarget() must be non-NULL or device will not
58 SkGpuDevice(GrContext*, GrTexture*);
60 virtual ~SkGpuDevice();
62 GrContext* context() const { return fContext; }
64 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
66 // overrides from SkBaseDevice
67 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE {
70 virtual int width() const SK_OVERRIDE {
71 return NULL == fRenderTarget ? 0 : fRenderTarget->width();
73 virtual int height() const SK_OVERRIDE {
74 return NULL == fRenderTarget ? 0 : fRenderTarget->height();
76 virtual bool isOpaque() const SK_OVERRIDE {
77 return NULL == fRenderTarget ? false
78 : kRGB_565_GrPixelConfig == fRenderTarget->config();
80 virtual SkBitmap::Config config() const SK_OVERRIDE;
82 virtual void clear(SkColor color) SK_OVERRIDE;
83 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
84 SkCanvas::Config8888 config8888) SK_OVERRIDE;
86 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
87 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
88 const SkPoint[], const SkPaint& paint) SK_OVERRIDE;
89 virtual void drawRect(const SkDraw&, const SkRect& r,
90 const SkPaint& paint) SK_OVERRIDE;
91 virtual void drawRRect(const SkDraw&, const SkRRect& r,
92 const SkPaint& paint) SK_OVERRIDE;
93 virtual void drawOval(const SkDraw&, const SkRect& oval,
94 const SkPaint& paint) SK_OVERRIDE;
95 virtual void drawPath(const SkDraw&, const SkPath& path,
96 const SkPaint& paint, const SkMatrix* prePathMatrix,
97 bool pathIsMutable) SK_OVERRIDE;
98 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
99 const SkMatrix&, const SkPaint&) SK_OVERRIDE;
100 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
101 const SkRect* srcOrNull, const SkRect& dst,
102 const SkPaint& paint,
103 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
104 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
105 int x, int y, const SkPaint& paint);
106 virtual void drawText(const SkDraw&, const void* text, size_t len,
107 SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
108 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
109 const SkScalar pos[], SkScalar constY,
110 int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
111 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
112 const SkPath& path, const SkMatrix* matrix,
113 const SkPaint&) SK_OVERRIDE;
114 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
115 const SkPoint verts[], const SkPoint texs[],
116 const SkColor colors[], SkXfermode* xmode,
117 const uint16_t indices[], int indexCount,
118 const SkPaint&) SK_OVERRIDE;
119 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
120 const SkPaint&) SK_OVERRIDE;
121 virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE;
123 virtual void flush() SK_OVERRIDE;
125 virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
126 virtual void onDetachFromCanvas() SK_OVERRIDE;
129 * Make's this device's rendertarget current in the underlying 3D API.
130 * Also implicitly flushes.
132 virtual void makeRenderTargetCurrent();
134 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE;
135 virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkMatrix&,
136 SkBitmap*, SkIPoint*) SK_OVERRIDE;
138 class SkAutoCachedTexture; // used internally
141 // overrides from SkBaseDevice
142 virtual bool onReadPixels(const SkBitmap& bitmap,
144 SkCanvas::Config8888 config8888) SK_OVERRIDE;
149 GrSkDrawProcs* fDrawProcs;
151 GrClipData fClipData;
153 GrTextContext* fMainTextContext;
154 GrTextContext* fFallbackTextContext;
156 // state for our render-target
157 GrRenderTarget* fRenderTarget;
160 // called from rt and tex cons
161 void initFromRenderTarget(GrContext*, GrRenderTarget*, bool cached);
163 // used by createCompatibleDevice
164 SkGpuDevice(GrContext*, GrTexture* texture, bool needClear);
166 // override from SkBaseDevice
167 virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config,
168 int width, int height,
170 Usage usage) SK_OVERRIDE;
172 virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
174 // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override
175 // SkDraw's matrix and draw in device coords.
176 void prepareDraw(const SkDraw&, bool forceIdentity);
179 * Implementation for both drawBitmap and drawBitmapRect.
181 void drawBitmapCommon(const SkDraw&,
182 const SkBitmap& bitmap,
183 const SkRect* srcRectPtr,
184 const SkSize* dstSizePtr, // ignored iff srcRectPtr == NULL
186 SkCanvas::DrawBitmapRectFlags flags);
189 * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's
190 * matrix, clip, and the device's render target has already been set on GrContext.
193 // The tileSize and clippedSrcRect will be valid only if true is returned.
194 bool shouldTileBitmap(const SkBitmap& bitmap,
195 const GrTextureParams& sampler,
196 const SkRect* srcRectPtr,
199 SkIRect* clippedSrcRect) const;
200 void internalDrawBitmap(const SkBitmap&,
202 const GrTextureParams& params,
203 const SkPaint& paint,
204 SkCanvas::DrawBitmapRectFlags flags,
206 void drawTiledBitmap(const SkBitmap& bitmap,
207 const SkRect& srcRect,
208 const SkIRect& clippedSrcRect,
209 const GrTextureParams& params,
210 const SkPaint& paint,
211 SkCanvas::DrawBitmapRectFlags flags,
215 typedef SkBitmapDevice INHERITED;