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" |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 14 | #include "SkRect.h" |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 15 | |
bsalomon | c0eb9b9 | 2014-09-29 14:20:11 -0700 | [diff] [blame] | 16 | class GrTexture; |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame^] | 17 | class GrRenderTarget; |
| 18 | struct SkImageInfo; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 19 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 20 | class GrSurface : public GrGpuResource { |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 21 | public: |
| 22 | SK_DECLARE_INST_COUNT(GrSurface); |
| 23 | |
| 24 | /** |
| 25 | * Retrieves the width of the surface. |
| 26 | * |
| 27 | * @return the width in texels |
| 28 | */ |
| 29 | int width() const { return fDesc.fWidth; } |
| 30 | |
| 31 | /** |
| 32 | * Retrieves the height of the surface. |
| 33 | * |
| 34 | * @return the height in texels |
| 35 | */ |
| 36 | int height() const { return fDesc.fHeight; } |
| 37 | |
commit-bot@chromium.org | 24ab3b0 | 2013-08-14 21:56:37 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Helper that gets the width and height of the surface as a bounding rectangle. |
| 40 | */ |
| 41 | void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()), |
| 42 | SkIntToScalar(this->height())); } |
| 43 | |
senorblanco@chromium.org | ef5dbe1 | 2013-01-28 16:42:38 +0000 | [diff] [blame] | 44 | GrSurfaceOrigin origin() const { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 45 | SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin); |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 46 | return fDesc.fOrigin; |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 50 | * Retrieves the pixel config specified when the surface was created. |
| 51 | * For render targets this can be kUnknown_GrPixelConfig |
| 52 | * if client asked us to render to a target that has a pixel |
| 53 | * config that isn't equivalent with one of our configs. |
| 54 | */ |
| 55 | GrPixelConfig config() const { return fDesc.fConfig; } |
| 56 | |
| 57 | /** |
| 58 | * Return the descriptor describing the surface |
| 59 | */ |
| 60 | const GrTextureDesc& desc() const { return fDesc; } |
| 61 | |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame^] | 62 | SkImageInfo info() const; |
| 63 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 64 | /** |
| 65 | * @return the texture associated with the surface, may be NULL. |
| 66 | */ |
| 67 | virtual GrTexture* asTexture() = 0; |
| 68 | virtual const GrTexture* asTexture() const = 0; |
| 69 | |
| 70 | /** |
| 71 | * @return the render target underlying this surface, may be NULL. |
| 72 | */ |
| 73 | virtual GrRenderTarget* asRenderTarget() = 0; |
| 74 | virtual const GrRenderTarget* asRenderTarget() const = 0; |
| 75 | |
| 76 | /** |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame^] | 77 | * Checks whether this GrSurface refers to the same GPU object as other. This |
| 78 | * catches the case where a GrTexture and GrRenderTarget refer to the same |
| 79 | * GPU memory. |
| 80 | */ |
| 81 | bool isSameAs(const GrSurface* other) const { |
| 82 | const GrRenderTarget* thisRT = this->asRenderTarget(); |
| 83 | if (thisRT) { |
| 84 | return thisRT == other->asRenderTarget(); |
| 85 | } else { |
| 86 | const GrTexture* thisTex = this->asTexture(); |
| 87 | SkASSERT(thisTex); // We must be one or the other |
| 88 | return thisTex == other->asTexture(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 93 | * Reads a rectangle of pixels from the surface. |
| 94 | * @param left left edge of the rectangle to read (inclusive) |
| 95 | * @param top top edge of the rectangle to read (inclusive) |
| 96 | * @param width width of rectangle to read in pixels. |
| 97 | * @param height height of rectangle to read in pixels. |
| 98 | * @param config the pixel config of the destination buffer |
| 99 | * @param buffer memory to read the rectangle into. |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 100 | * @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] | 101 | * packed. |
| 102 | * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 103 | * |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 104 | * @return true if the read succeeded, false if not. The read can fail because of an unsupported |
| 105 | * pixel config. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 106 | */ |
| 107 | virtual bool readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 108 | GrPixelConfig config, |
| 109 | void* buffer, |
| 110 | size_t rowBytes = 0, |
| 111 | uint32_t pixelOpsFlags = 0) = 0; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 112 | |
| 113 | /** |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 114 | * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified |
| 115 | * rectangle. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 116 | * @param left left edge of the rectangle to write (inclusive) |
| 117 | * @param top top edge of the rectangle to write (inclusive) |
| 118 | * @param width width of rectangle to write in pixels. |
| 119 | * @param height height of rectangle to write in pixels. |
| 120 | * @param config the pixel config of the source buffer |
| 121 | * @param buffer memory to read the rectangle from. |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 122 | * @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] | 123 | * packed. |
| 124 | * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum. |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 125 | */ |
| 126 | virtual void writePixels(int left, int top, int width, int height, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 127 | GrPixelConfig config, |
| 128 | const void* buffer, |
| 129 | size_t rowBytes = 0, |
| 130 | uint32_t pixelOpsFlags = 0) = 0; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 131 | |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame^] | 132 | /** |
| 133 | * Write the contents of the surface to a PNG. Returns true if successful. |
| 134 | * @param filename Full path to desired file |
| 135 | */ |
bsalomon | c0eb9b9 | 2014-09-29 14:20:11 -0700 | [diff] [blame] | 136 | bool savePixels(const char* filename); |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame^] | 137 | |
bsalomon | 8d034a1 | 2014-09-22 12:21:08 -0700 | [diff] [blame] | 138 | bool hasPendingRead() const; |
| 139 | bool hasPendingWrite() const; |
| 140 | bool hasPendingIO() const; |
| 141 | |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame^] | 142 | protected: |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 143 | GrSurface(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) |
bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame] | 144 | : INHERITED(gpu, isWrapped) |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 145 | , fDesc(desc) { |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 146 | } |
| 147 | |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 148 | GrTextureDesc fDesc; |
| 149 | |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 150 | private: |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 151 | typedef GrGpuResource INHERITED; |
robertphillips@google.com | 7d501ab | 2012-06-21 21:09:06 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
robertphillips | b06e5a2 | 2014-09-30 06:58:20 -0700 | [diff] [blame^] | 154 | #endif // GrSurface_DEFINED |