robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 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" |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 13 | #include "GrGpuResource.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 14 | #include "SkImageInfo.h" |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 15 | #include "SkRect.h" |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 16 | |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame] | 17 | class GrRenderTarget; |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 18 | class GrSurfacePriv; |
| 19 | class GrTexture; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 20 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 21 | class GrSurface : public GrGpuResource { |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 22 | public: |
| 23 | SK_DECLARE_INST_COUNT(GrSurface); |
| 24 | |
| 25 | /** |
| 26 | * Retrieves the width of the surface. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 27 | */ |
| 28 | int width() const { return fDesc.fWidth; } |
| 29 | |
| 30 | /** |
| 31 | * Retrieves the height of the surface. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 32 | */ |
| 33 | int height() const { return fDesc.fHeight; } |
| 34 | |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 35 | /** |
| 36 | * Helper that gets the width and height of the surface as a bounding rectangle. |
| 37 | */ |
| 38 | void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()), |
| 39 | SkIntToScalar(this->height())); } |
| 40 | |
senorblanco@chromium.org | ef5dbe1 | 2013-01-28 16:42:38 +0000 | [diff] [blame] | 41 | GrSurfaceOrigin origin() const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 42 | SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin); |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 43 | return fDesc.fOrigin; |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 47 | * Retrieves the pixel config specified when the surface was created. |
| 48 | * For render targets this can be kUnknown_GrPixelConfig |
| 49 | * if client asked us to render to a target that has a pixel |
| 50 | * config that isn't equivalent with one of our configs. |
| 51 | */ |
| 52 | GrPixelConfig config() const { return fDesc.fConfig; } |
| 53 | |
| 54 | /** |
| 55 | * Return the descriptor describing the surface |
| 56 | */ |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 57 | const GrSurfaceDesc& desc() const { return fDesc; } |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * @return the texture associated with the surface, may be NULL. |
| 61 | */ |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 62 | virtual GrTexture* asTexture() { return NULL; } |
| 63 | virtual const GrTexture* asTexture() const { return NULL; } |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * @return the render target underlying this surface, may be NULL. |
| 67 | */ |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 68 | virtual GrRenderTarget* asRenderTarget() { return NULL; } |
| 69 | virtual const GrRenderTarget* asRenderTarget() const { return NULL; } |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Reads a rectangle of pixels from the surface. |
| 73 | * @param left left edge of the rectangle to read (inclusive) |
| 74 | * @param top top edge of the rectangle to read (inclusive) |
| 75 | * @param width width of rectangle to read in pixels. |
| 76 | * @param height height of rectangle to read in pixels. |
| 77 | * @param config the pixel config of the destination buffer |
| 78 | * @param buffer memory to read the rectangle into. |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 79 | * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 80 | * packed. |
| 81 | * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 82 | * |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 83 | * @return true if the read succeeded, false if not. The read can fail because of an unsupported |
| 84 | * pixel config. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 85 | */ |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 86 | bool readPixels(int left, int top, int width, int height, |
| 87 | GrPixelConfig config, |
| 88 | void* buffer, |
| 89 | size_t rowBytes = 0, |
| 90 | uint32_t pixelOpsFlags = 0); |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 93 | * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified |
| 94 | * rectangle. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 95 | * @param left left edge of the rectangle to write (inclusive) |
| 96 | * @param top top edge of the rectangle to write (inclusive) |
| 97 | * @param width width of rectangle to write in pixels. |
| 98 | * @param height height of rectangle to write in pixels. |
| 99 | * @param config the pixel config of the source buffer |
| 100 | * @param buffer memory to read the rectangle from. |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 101 | * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 102 | * packed. |
| 103 | * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum. |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 104 | * |
| 105 | * @return true if the read succeeded, false if not. The read can fail because of an unsupported |
| 106 | * pixel config. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 107 | */ |
bsalomon | 81beccc | 2014-10-13 12:32:55 -0700 | [diff] [blame] | 108 | bool writePixels(int left, int top, int width, int height, |
| 109 | GrPixelConfig config, |
| 110 | const void* buffer, |
| 111 | size_t rowBytes = 0, |
| 112 | uint32_t pixelOpsFlags = 0); |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 113 | |
bsalomon | f80bfed | 2014-10-07 05:56:02 -0700 | [diff] [blame] | 114 | /** |
| 115 | * After this returns any pending writes to the surface will be issued to the backend 3D API. |
| 116 | */ |
| 117 | void flushWrites(); |
| 118 | |
bsalomon | 87a94eb | 2014-11-03 14:28:32 -0800 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * After this returns any pending writes to the surface will be issued to the backend 3D API and |
| 122 | * if the surface has MSAA it will be resolved. |
| 123 | */ |
| 124 | void prepareForExternalRead(); |
| 125 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 126 | /** Access methods that are only to be used within Skia code. */ |
| 127 | inline GrSurfacePriv surfacePriv(); |
| 128 | inline const GrSurfacePriv surfacePriv() const; |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame] | 129 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 130 | typedef void* ReleaseCtx; |
| 131 | typedef void (*ReleaseProc)(ReleaseCtx); |
| 132 | |
| 133 | void setRelease(ReleaseProc proc, ReleaseCtx ctx) { |
| 134 | fReleaseProc = proc; |
| 135 | fReleaseCtx = ctx; |
| 136 | } |
| 137 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 138 | protected: |
| 139 | // Methods made available via GrSurfacePriv |
| 140 | SkImageInfo info() const; |
| 141 | bool savePixels(const char* filename); |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 142 | bool hasPendingRead() const; |
| 143 | bool hasPendingWrite() const; |
| 144 | bool hasPendingIO() const; |
| 145 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 146 | // Provides access to methods that should be public within Skia code. |
| 147 | friend class GrSurfacePriv; |
| 148 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 149 | GrSurface(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc) |
| 150 | : INHERITED(gpu, lifeCycle) |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 151 | , fDesc(desc) |
| 152 | , fReleaseProc(NULL) |
| 153 | , fReleaseCtx(NULL) |
| 154 | {} |
| 155 | |
| 156 | ~GrSurface() override { |
| 157 | // check that invokeReleaseProc has been called (if needed) |
| 158 | SkASSERT(NULL == fReleaseProc); |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 159 | } |
| 160 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 161 | GrSurfaceDesc fDesc; |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 162 | |
reed | 35a5261 | 2015-06-18 14:05:07 -0700 | [diff] [blame^] | 163 | void onRelease() override; |
| 164 | void onAbandon() override; |
| 165 | |
| 166 | private: |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 167 | void invokeReleaseProc() { |
| 168 | if (fReleaseProc) { |
| 169 | fReleaseProc(fReleaseCtx); |
| 170 | fReleaseProc = NULL; |
| 171 | } |
| 172 | } |
reed | 35a5261 | 2015-06-18 14:05:07 -0700 | [diff] [blame^] | 173 | |
reed | de49988 | 2015-06-18 13:41:40 -0700 | [diff] [blame] | 174 | ReleaseProc fReleaseProc; |
| 175 | ReleaseCtx fReleaseCtx; |
| 176 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 177 | typedef GrGpuResource INHERITED; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 180 | #endif |