Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrSurface.h
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 
9 #ifndef GrSurface_DEFINED
10 #define GrSurface_DEFINED
11 
12 #include "GrTypes.h"
13 #include "GrGpuResource.h"
14 #include "SkImageInfo.h"
15 #include "SkRect.h"
16 
17 class GrRenderTarget;
18 class GrSurfacePriv;
19 class GrTexture;
20 
21 class SK_API GrSurface : public GrGpuResource {
22 public:
26  int width() const { return fDesc.fWidth; }
27 
31  int height() const { return fDesc.fHeight; }
32 
36  void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()),
37  SkIntToScalar(this->height())); }
38 
39  GrSurfaceOrigin origin() const {
40  SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
41  return fDesc.fOrigin;
42  }
43 
50  GrPixelConfig config() const { return fDesc.fConfig; }
51 
55  const GrSurfaceDesc& desc() const { return fDesc; }
56 
60  virtual GrTexture* asTexture() { return NULL; }
61  virtual const GrTexture* asTexture() const { return NULL; }
62 
66  virtual GrRenderTarget* asRenderTarget() { return NULL; }
67  virtual const GrRenderTarget* asRenderTarget() const { return NULL; }
68 
84  bool readPixels(int left, int top, int width, int height,
85  GrPixelConfig config,
86  void* buffer,
87  size_t rowBytes = 0,
88  uint32_t pixelOpsFlags = 0);
89 
106  bool writePixels(int left, int top, int width, int height,
107  GrPixelConfig config,
108  const void* buffer,
109  size_t rowBytes = 0,
110  uint32_t pixelOpsFlags = 0);
111 
115  void flushWrites();
116 
118  inline GrSurfacePriv surfacePriv();
119  inline const GrSurfacePriv surfacePriv() const;
120 
121  typedef void* ReleaseCtx;
122  typedef void (*ReleaseProc)(ReleaseCtx);
123 
124  void setRelease(ReleaseProc proc, ReleaseCtx ctx) {
125  fReleaseProc = proc;
126  fReleaseCtx = ctx;
127  }
128 
129  static size_t WorstCaseSize(const GrSurfaceDesc& desc);
130 
131 protected:
132  // Methods made available via GrSurfacePriv
133  bool savePixels(const char* filename);
134  bool hasPendingRead() const;
135  bool hasPendingWrite() const;
136  bool hasPendingIO() const;
137 
138  // Provides access to methods that should be public within Skia code.
139  friend class GrSurfacePriv;
140 
141  GrSurface(GrGpu* gpu, const GrSurfaceDesc& desc)
142  : INHERITED(gpu)
143  , fDesc(desc)
144  , fReleaseProc(NULL)
145  , fReleaseCtx(NULL)
146  {}
147 
148  ~GrSurface() override {
149  // check that invokeReleaseProc has been called (if needed)
150  SkASSERT(NULL == fReleaseProc);
151  }
152 
153  GrSurfaceDesc fDesc;
154 
155  void onRelease() override;
156  void onAbandon() override;
157 
158 private:
159  void invokeReleaseProc() {
160  if (fReleaseProc) {
161  fReleaseProc(fReleaseCtx);
162  fReleaseProc = NULL;
163  }
164  }
165 
166  ReleaseProc fReleaseProc;
167  ReleaseCtx fReleaseCtx;
168 
169  typedef GrGpuResource INHERITED;
170 };
171 
172 #endif
Base class for objects that can be kept in the GrResourceCache.
Definition: GrGpuResource.h:140
virtual void onAbandon()
Overridden to abandon any internal handles, ptrs, etc to backend API resources.
Definition: GrGpuResource.h:255
Describes a surface to be created.
Definition: GrTypes.h:467
virtual void onRelease()
Overridden to free GPU resources in the backend API.
Definition: GrGpuResource.h:251
virtual GrRenderTarget * asRenderTarget()
Definition: GrSurface.h:66
virtual GrTexture * asTexture()
Definition: GrSurface.h:60
Definition: GrSurface.h:21
int width() const
Retrieves the width of the surface.
Definition: GrSurface.h:26
void getBoundsRect(SkRect *rect) const
Helper that gets the width and height of the surface as a bounding rectangle.
Definition: GrSurface.h:36
int height() const
Retrieves the height of the surface.
Definition: GrSurface.h:31
const GrSurfaceDesc & desc() const
Return the descriptor describing the surface.
Definition: GrSurface.h:55
Definition: SkRect.h:390
GrPixelConfig config() const
Retrieves the pixel config specified when the surface was created.
Definition: GrSurface.h:50
GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
Definition: GrRenderTarget.h:26
Definition: GrTexture.h:19