blob: 65f1e0f690cc0decd9643dc94ba3098ce9951687 [file] [log] [blame]
robertphillips@google.com7d501ab2012-06-21 21:09:06 +00001/*
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"
bsalomon6d3fe022014-07-25 08:35:45 -070013#include "GrGpuResource.h"
bsalomonafbf2d62014-09-30 12:18:44 -070014#include "SkImageInfo.h"
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000015#include "SkRect.h"
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000016
robertphillipsb06e5a22014-09-30 06:58:20 -070017class GrRenderTarget;
bsalomonafbf2d62014-09-30 12:18:44 -070018class GrSurfacePriv;
19class GrTexture;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000020
bsalomoncc97ece2015-07-08 07:53:10 -070021class SK_API GrSurface : public GrGpuResource {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000022public:
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000023 /**
24 * Retrieves the width of the surface.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000025 */
26 int width() const { return fDesc.fWidth; }
27
28 /**
29 * Retrieves the height of the surface.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000030 */
31 int height() const { return fDesc.fHeight; }
32
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000033 /**
34 * Helper that gets the width and height of the surface as a bounding rectangle.
35 */
robertphillips13a7eee2016-08-31 15:06:24 -070036 SkRect getBoundsRect() const { return SkRect::MakeIWH(this->width(), this->height()); }
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +000037
senorblanco@chromium.orgef5dbe12013-01-28 16:42:38 +000038 GrSurfaceOrigin origin() const {
Robert Phillipsc589b0b2017-04-17 07:53:07 -040039 SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin ||
40 kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000041 return fDesc.fOrigin;
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000042 }
43
44 /**
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000045 * Retrieves the pixel config specified when the surface was created.
46 * For render targets this can be kUnknown_GrPixelConfig
47 * if client asked us to render to a target that has a pixel
48 * config that isn't equivalent with one of our configs.
49 */
50 GrPixelConfig config() const { return fDesc.fConfig; }
51
52 /**
53 * Return the descriptor describing the surface
54 */
bsalomonf2703d82014-10-28 14:33:06 -070055 const GrSurfaceDesc& desc() const { return fDesc; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000056
57 /**
Robert Phillipsc589b0b2017-04-17 07:53:07 -040058 * @return the texture associated with the surface, may be null.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000059 */
Robert Phillipsc589b0b2017-04-17 07:53:07 -040060 virtual GrTexture* asTexture() { return nullptr; }
61 virtual const GrTexture* asTexture() const { return nullptr; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000062
63 /**
Robert Phillipsc589b0b2017-04-17 07:53:07 -040064 * @return the render target underlying this surface, may be null.
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000065 */
Robert Phillipsc589b0b2017-04-17 07:53:07 -040066 virtual GrRenderTarget* asRenderTarget() { return nullptr; }
67 virtual const GrRenderTarget* asRenderTarget() const { return nullptr; }
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000068
bsalomonafbf2d62014-09-30 12:18:44 -070069 /** Access methods that are only to be used within Skia code. */
70 inline GrSurfacePriv surfacePriv();
71 inline const GrSurfacePriv surfacePriv() const;
robertphillipsb06e5a22014-09-30 06:58:20 -070072
reedde499882015-06-18 13:41:40 -070073 typedef void* ReleaseCtx;
74 typedef void (*ReleaseProc)(ReleaseCtx);
mtklein2766c002015-06-26 11:45:03 -070075
reedde499882015-06-18 13:41:40 -070076 void setRelease(ReleaseProc proc, ReleaseCtx ctx) {
77 fReleaseProc = proc;
78 fReleaseCtx = ctx;
79 }
80
Robert Phillipsb4460882016-11-17 14:43:51 -050081 static size_t WorstCaseSize(const GrSurfaceDesc& desc, bool useNextPow2 = false);
82 static size_t ComputeSize(const GrSurfaceDesc& desc, int colorSamplesPerPixel,
83 bool hasMIPMaps, bool useNextPow2 = false);
Robert Phillipsd6214d42016-11-07 08:23:48 -050084
bsalomonafbf2d62014-09-30 12:18:44 -070085protected:
86 // Methods made available via GrSurfacePriv
bsalomon8d034a12014-09-22 12:21:08 -070087 bool hasPendingRead() const;
88 bool hasPendingWrite() const;
89 bool hasPendingIO() const;
90
bsalomonafbf2d62014-09-30 12:18:44 -070091 // Provides access to methods that should be public within Skia code.
92 friend class GrSurfacePriv;
93
kkinnunen2e6055b2016-04-22 01:48:29 -070094 GrSurface(GrGpu* gpu, const GrSurfaceDesc& desc)
95 : INHERITED(gpu)
reedde499882015-06-18 13:41:40 -070096 , fDesc(desc)
Robert Phillipsc589b0b2017-04-17 07:53:07 -040097 , fReleaseProc(nullptr)
98 , fReleaseCtx(nullptr) {
robertphillips@google.com7d501ab2012-06-21 21:09:06 +000099 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400100 ~GrSurface() override;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000101
bsalomonf2703d82014-10-28 14:33:06 -0700102 GrSurfaceDesc fDesc;
bsalomon@google.com2d0bade2012-10-26 19:01:17 +0000103
reed35a52612015-06-18 14:05:07 -0700104 void onRelease() override;
105 void onAbandon() override;
106
107private:
reedde499882015-06-18 13:41:40 -0700108 void invokeReleaseProc() {
109 if (fReleaseProc) {
110 fReleaseProc(fReleaseCtx);
Robert Phillipsc589b0b2017-04-17 07:53:07 -0400111 fReleaseProc = nullptr;
reedde499882015-06-18 13:41:40 -0700112 }
113 }
mtklein2766c002015-06-26 11:45:03 -0700114
reedde499882015-06-18 13:41:40 -0700115 ReleaseProc fReleaseProc;
116 ReleaseCtx fReleaseCtx;
117
bsalomon6d3fe022014-07-25 08:35:45 -0700118 typedef GrGpuResource INHERITED;
robertphillips@google.com7d501ab2012-06-21 21:09:06 +0000119};
120
bsalomonafbf2d62014-09-30 12:18:44 -0700121#endif