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 kDFFonts_Flag = 1 << 2, //!< Surface should render fonts using signed distance fields 36 }; 37 38 /** 39 * Creates an SkGpuDevice from a GrSurface. This will fail if the surface is not a render 40 * target. The caller owns a ref on the returned device. If the surface is cached, 41 * the kCached_Flag should be specified to make the device responsible for unlocking 42 * the surface when it is released. 43 */ 44 static SkGpuDevice* Create(GrSurface* surface, unsigned flags = 0); 45 46 /** 47 * New device that will create an offscreen renderTarget based on the 48 * ImageInfo and sampleCount. The device's storage will not 49 * count against the GrContext's texture cache budget. The device's pixels 50 * will be uninitialized. On failure, returns NULL. 51 */ 52 static SkGpuDevice* Create(GrContext*, const SkImageInfo&, int sampleCount); 53 54 /** 55 * DEPRECATED -- need to make this private, call Create(surface) 56 * New device that will render to the specified renderTarget. 57 */ 58 SkGpuDevice(GrContext*, GrRenderTarget*, unsigned flags = 0); 59 60 /** 61 * DEPRECATED -- need to make this private, call Create(surface) 62 * New device that will render to the texture (as a rendertarget). 63 * The GrTexture's asRenderTarget() must be non-NULL or device will not 64 * function. 65 */ 66 SkGpuDevice(GrContext*, GrTexture*, unsigned flags = 0); 67 68 virtual ~SkGpuDevice(); 69 70 GrContext* context() const { return fContext; } 71 72 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE; 73 74 virtual SkImageInfo imageInfo() const SK_OVERRIDE { 75 return fRenderTarget ? fRenderTarget->info() : SkImageInfo::MakeUnknown(); 76 } 77 78 virtual void clear(SkColor color) SK_OVERRIDE; 79 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE; 80 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, 81 const SkPoint[], const SkPaint& paint) SK_OVERRIDE; 82 virtual void drawRect(const SkDraw&, const SkRect& r, 83 const SkPaint& paint) SK_OVERRIDE; 84 virtual void drawRRect(const SkDraw&, const SkRRect& r, 85 const SkPaint& paint) SK_OVERRIDE; 86 virtual void drawDRRect(const SkDraw& draw, const SkRRect& outer, 87 const SkRRect& inner, const SkPaint& paint) SK_OVERRIDE; 88 virtual void drawOval(const SkDraw&, const SkRect& oval, 89 const SkPaint& paint) SK_OVERRIDE; 90 virtual void drawPath(const SkDraw&, const SkPath& path, 91 const SkPaint& paint, const SkMatrix* prePathMatrix, 92 bool pathIsMutable) SK_OVERRIDE; 93 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, 94 const SkMatrix&, const SkPaint&) SK_OVERRIDE; 95 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, 96 const SkRect* srcOrNull, const SkRect& dst, 97 const SkPaint& paint, 98 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE; 99 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, 100 int x, int y, const SkPaint& paint); 101 virtual void drawText(const SkDraw&, const void* text, size_t len, 102 SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE; 103 virtual void drawPosText(const SkDraw&, const void* text, size_t len, 104 const SkScalar pos[], SkScalar constY, 105 int scalarsPerPos, const SkPaint&) SK_OVERRIDE; 106 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, 107 const SkPath& path, const SkMatrix* matrix, 108 const SkPaint&) SK_OVERRIDE; 109 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, 110 const SkPoint verts[], const SkPoint texs[], 111 const SkColor colors[], SkXfermode* xmode, 112 const uint16_t indices[], int indexCount, 113 const SkPaint&) SK_OVERRIDE; 114 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, 115 const SkPaint&) SK_OVERRIDE; 116 virtual bool filterTextFlags(const SkPaint&, TextFlags*) SK_OVERRIDE; 117 118 virtual void flush() SK_OVERRIDE; 119 120 virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE; 121 virtual void onDetachFromCanvas() SK_OVERRIDE; 122 123 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE; 124 125 /** 126 * Make's this device's rendertarget current in the underlying 3D API. 127 * Also implicitly flushes. 128 */ 129 virtual void makeRenderTargetCurrent(); 130 131 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE; 132 virtual bool filterImage(const SkImageFilter*, const SkBitmap&, 133 const SkImageFilter::Context&, 134 SkBitmap*, SkIPoint*) SK_OVERRIDE; 135 136 class SkAutoCachedTexture; // used internally 137 138 139protected: 140 virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int, int) SK_OVERRIDE; 141 virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) SK_OVERRIDE; 142 143 /** PRIVATE / EXPERIMENTAL -- do not call */ 144 virtual void EXPERIMENTAL_optimize(const SkPicture* picture) SK_OVERRIDE; 145 /** PRIVATE / EXPERIMENTAL -- do not call */ 146 virtual void EXPERIMENTAL_purge(const SkPicture* picture) SK_OVERRIDE; 147 /** PRIVATE / EXPERIMENTAL -- do not call */ 148 virtual bool EXPERIMENTAL_drawPicture(SkCanvas* canvas, const SkPicture* picture) SK_OVERRIDE; 149 150private: 151 GrContext* fContext; 152 153 GrSkDrawProcs* fDrawProcs; 154 155 GrClipData fClipData; 156 157 GrTextContext* fMainTextContext; 158 GrTextContext* fFallbackTextContext; 159 160 // state for our render-target 161 GrRenderTarget* fRenderTarget; 162 bool fNeedClear; 163 164 // called from rt and tex cons 165 void initFromRenderTarget(GrContext*, GrRenderTarget*, unsigned flags); 166 167 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE; 168 169 virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE; 170 171 // sets the render target, clip, and matrix on GrContext. Use forceIdenity to override 172 // SkDraw's matrix and draw in device coords. 173 void prepareDraw(const SkDraw&, bool forceIdentity); 174 175 /** 176 * Implementation for both drawBitmap and drawBitmapRect. 177 */ 178 void drawBitmapCommon(const SkDraw&, 179 const SkBitmap& bitmap, 180 const SkRect* srcRectPtr, 181 const SkSize* dstSizePtr, // ignored iff srcRectPtr == NULL 182 const SkPaint&, 183 SkCanvas::DrawBitmapRectFlags flags); 184 185 /** 186 * Helper functions called by drawBitmapCommon. By the time these are called the SkDraw's 187 * matrix, clip, and the device's render target has already been set on GrContext. 188 */ 189 190 // The tileSize and clippedSrcRect will be valid only if true is returned. 191 bool shouldTileBitmap(const SkBitmap& bitmap, 192 const GrTextureParams& sampler, 193 const SkRect* srcRectPtr, 194 int maxTileSize, 195 int* tileSize, 196 SkIRect* clippedSrcRect) const; 197 void internalDrawBitmap(const SkBitmap&, 198 const SkRect&, 199 const GrTextureParams& params, 200 const SkPaint& paint, 201 SkCanvas::DrawBitmapRectFlags flags, 202 bool bicubic, 203 bool needsTextureDomain); 204 void drawTiledBitmap(const SkBitmap& bitmap, 205 const SkRect& srcRect, 206 const SkIRect& clippedSrcRect, 207 const GrTextureParams& params, 208 const SkPaint& paint, 209 SkCanvas::DrawBitmapRectFlags flags, 210 int tileSize, 211 bool bicubic); 212 213 bool drawDashLine(const SkPoint pts[2], const SkPaint& paint); 214 215 static SkPicture::AccelData::Key ComputeAccelDataKey(); 216 217 typedef SkBitmapDevice INHERITED; 218}; 219 220#endif 221