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:
23     /**
24      * Retrieves the width of the surface.
25      */
width()26     int width() const { return fDesc.fWidth; }
27 
28     /**
29      * Retrieves the height of the surface.
30      */
height()31     int height() const { return fDesc.fHeight; }
32 
33     /**
34      * Helper that gets the width and height of the surface as a bounding rectangle.
35      */
getBoundsRect()36     SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
37 
origin()38     GrSurfaceOrigin origin() const {
39         SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
40         return fDesc.fOrigin;
41     }
42 
43     /**
44      * Retrieves the pixel config specified when the surface was created.
45      * For render targets this can be kUnknown_GrPixelConfig
46      * if client asked us to render to a target that has a pixel
47      * config that isn't equivalent with one of our configs.
48      */
config()49     GrPixelConfig config() const { return fDesc.fConfig; }
50 
51     /**
52      * Return the descriptor describing the surface
53      */
desc()54     const GrSurfaceDesc& desc() const { return fDesc; }
55 
56     /**
57      * @return the texture associated with the surface, may be NULL.
58      */
asTexture()59     virtual GrTexture* asTexture() { return NULL; }
asTexture()60     virtual const GrTexture* asTexture() const { return NULL; }
61 
62     /**
63      * @return the render target underlying this surface, may be NULL.
64      */
asRenderTarget()65     virtual GrRenderTarget* asRenderTarget() { return NULL; }
asRenderTarget()66     virtual const GrRenderTarget* asRenderTarget() const { return NULL; }
67 
68     /**
69      * Reads a rectangle of pixels from the surface.
70      * @param left          left edge of the rectangle to read (inclusive)
71      * @param top           top edge of the rectangle to read (inclusive)
72      * @param width         width of rectangle to read in pixels.
73      * @param height        height of rectangle to read in pixels.
74      * @param config        the pixel config of the destination buffer
75      * @param buffer        memory to read the rectangle into.
76      * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
77      *                      packed.
78      * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
79      *
80      * @return true if the read succeeded, false if not. The read can fail because of an unsupported
81      *              pixel config.
82      */
83     bool readPixels(int left, int top, int width, int height,
84                     GrPixelConfig config,
85                     void* buffer,
86                     size_t rowBytes = 0,
87                     uint32_t pixelOpsFlags = 0);
88 
89     /**
90      * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
91      * rectangle.
92      * @param left          left edge of the rectangle to write (inclusive)
93      * @param top           top edge of the rectangle to write (inclusive)
94      * @param width         width of rectangle to write in pixels.
95      * @param height        height of rectangle to write in pixels.
96      * @param config        the pixel config of the source buffer
97      * @param buffer        memory to read the rectangle from.
98      * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
99      *                      packed.
100      * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
101      *
102      * @return true if the write succeeded, false if not. The write can fail because of an
103      *              unsupported pixel config.
104      */
105     bool writePixels(int left, int top, int width, int height,
106                      GrPixelConfig config,
107                      const void* buffer,
108                      size_t rowBytes = 0,
109                      uint32_t pixelOpsFlags = 0);
110 
111     /**
112      * After this returns any pending writes to the surface will be issued to the backend 3D API.
113      */
114     void flushWrites();
115 
116     /** Access methods that are only to be used within Skia code. */
117     inline GrSurfacePriv surfacePriv();
118     inline const GrSurfacePriv surfacePriv() const;
119 
120     typedef void* ReleaseCtx;
121     typedef void (*ReleaseProc)(ReleaseCtx);
122 
setRelease(ReleaseProc proc,ReleaseCtx ctx)123     void setRelease(ReleaseProc proc, ReleaseCtx ctx) {
124         fReleaseProc = proc;
125         fReleaseCtx = ctx;
126     }
127 
128     static size_t WorstCaseSize(const GrSurfaceDesc& desc);
129 
130 protected:
131     // Methods made available via GrSurfacePriv
132     bool savePixels(const char* filename);
133     bool hasPendingRead() const;
134     bool hasPendingWrite() const;
135     bool hasPendingIO() const;
136 
137     // Provides access to methods that should be public within Skia code.
138     friend class GrSurfacePriv;
139 
GrSurface(GrGpu * gpu,const GrSurfaceDesc & desc)140     GrSurface(GrGpu* gpu, const GrSurfaceDesc& desc)
141         : INHERITED(gpu)
142         , fDesc(desc)
143         , fReleaseProc(NULL)
144         , fReleaseCtx(NULL)
145     {}
146 
~GrSurface()147     ~GrSurface() override {
148         // check that invokeReleaseProc has been called (if needed)
149         SkASSERT(NULL == fReleaseProc);
150     }
151 
152     GrSurfaceDesc fDesc;
153 
154     void onRelease() override;
155     void onAbandon() override;
156 
157 private:
invokeReleaseProc()158     void invokeReleaseProc() {
159         if (fReleaseProc) {
160             fReleaseProc(fReleaseCtx);
161             fReleaseProc = NULL;
162         }
163     }
164 
165     ReleaseProc fReleaseProc;
166     ReleaseCtx  fReleaseCtx;
167 
168     typedef GrGpuResource INHERITED;
169 };
170 
171 #endif
172