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 #ifndef GrSurface_DEFINED 9 #define GrSurface_DEFINED 10 11 #include "GrTypes.h" 12 #include "GrBackendSurface.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 fWidth; } 27 28 /** 29 * Retrieves the height of the surface. 30 */ height()31 int height() const { return 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 38 /** 39 * Retrieves the pixel config specified when the surface was created. 40 * For render targets this can be kUnknown_GrPixelConfig 41 * if client asked us to render to a target that has a pixel 42 * config that isn't equivalent with one of our configs. 43 */ config()44 GrPixelConfig config() const { return fConfig; } 45 46 virtual GrBackendFormat backendFormat() const = 0; 47 48 /** 49 * @return the texture associated with the surface, may be null. 50 */ asTexture()51 virtual GrTexture* asTexture() { return nullptr; } asTexture()52 virtual const GrTexture* asTexture() const { return nullptr; } 53 54 /** 55 * @return the render target underlying this surface, may be null. 56 */ asRenderTarget()57 virtual GrRenderTarget* asRenderTarget() { return nullptr; } asRenderTarget()58 virtual const GrRenderTarget* asRenderTarget() const { return nullptr; } 59 60 /** Access methods that are only to be used within Skia code. */ 61 inline GrSurfacePriv surfacePriv(); 62 inline const GrSurfacePriv surfacePriv() const; 63 64 static size_t WorstCaseSize(const GrSurfaceDesc& desc, bool useNextPow2 = false); 65 static size_t ComputeSize(GrPixelConfig config, int width, int height, int colorSamplesPerPixel, 66 GrMipMapped, bool useNextPow2 = false); 67 68 /** 69 * The pixel values of this surface cannot be modified (e.g. doesn't support write pixels or 70 * MIP map level regen). 71 */ readOnly()72 bool readOnly() const { return fSurfaceFlags & GrInternalSurfaceFlags::kReadOnly; } 73 74 protected: setHasMixedSamples()75 void setHasMixedSamples() { 76 SkASSERT(this->asRenderTarget()); 77 fSurfaceFlags |= GrInternalSurfaceFlags::kMixedSampled; 78 } hasMixedSamples()79 bool hasMixedSamples() const { return fSurfaceFlags & GrInternalSurfaceFlags::kMixedSampled; } 80 setGLRTFBOIDIs0()81 void setGLRTFBOIDIs0() { 82 SkASSERT(this->asRenderTarget()); 83 fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0; 84 } glRTFBOIDis0()85 bool glRTFBOIDis0() const { 86 return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0; 87 } 88 setReadOnly()89 void setReadOnly() { 90 SkASSERT(!this->asRenderTarget()); 91 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly; 92 } 93 94 // Methods made available via GrSurfacePriv 95 bool hasPendingRead() const; 96 bool hasPendingWrite() const; 97 bool hasPendingIO() const; 98 99 // Provides access to methods that should be public within Skia code. 100 friend class GrSurfacePriv; 101 GrSurface(GrGpu * gpu,const GrSurfaceDesc & desc)102 GrSurface(GrGpu* gpu, const GrSurfaceDesc& desc) 103 : INHERITED(gpu) 104 , fConfig(desc.fConfig) 105 , fWidth(desc.fWidth) 106 , fHeight(desc.fHeight) 107 , fSurfaceFlags(GrInternalSurfaceFlags::kNone) { 108 } 109 ~GrSurface()110 ~GrSurface() override {} 111 112 void onRelease() override; 113 void onAbandon() override; 114 115 private: getResourceType()116 const char* getResourceType() const override { return "Surface"; } 117 118 GrPixelConfig fConfig; 119 int fWidth; 120 int fHeight; 121 GrInternalSurfaceFlags fSurfaceFlags; 122 123 typedef GrGpuResource INHERITED; 124 }; 125 126 #endif 127