Refactor createTexture and onCreateTexture

BUG=skia:

Review URL: https://codereview.chromium.org/1102663002
diff --git a/src/gpu/GrGpu.cpp b/src/gpu/GrGpu.cpp
index 88e2bb9..716c023 100644
--- a/src/gpu/GrGpu.cpp
+++ b/src/gpu/GrGpu.cpp
@@ -37,8 +37,25 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted,
+namespace {
+
+GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
+    // By default, GrRenderTargets are GL's normal orientation so that they
+    // can be drawn to by the outside world without the client having
+    // to render upside down.
+    if (kDefault_GrSurfaceOrigin == origin) {
+        return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
+    } else {
+        return origin;
+    }
+}
+
+}
+
+GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted,
                                 const void* srcData, size_t rowBytes) {
+    GrSurfaceDesc desc = origDesc;
+
     if (!this->caps()->isConfigTexturable(desc.fConfig)) {
         return NULL;
     }
@@ -49,9 +66,32 @@
     }
 
     GrTexture *tex = NULL;
+
+    if (isRT) {
+        int maxRTSize = this->caps()->maxRenderTargetSize();
+        if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
+            return NULL;
+        }
+    } else {
+        int maxSize = this->caps()->maxTextureSize();
+        if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
+            return NULL;
+        }
+    }
+
+    GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
+                                                    GrGpuResource::kUncached_LifeCycle;
+
+    desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
+    // Attempt to catch un- or wrongly initialized sample counts;
+    SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
+
+    desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
+
     if (GrPixelConfigIsCompressed(desc.fConfig)) {
         // We shouldn't be rendering into this
-        SkASSERT((desc.fFlags & kRenderTarget_GrSurfaceFlag) == 0);
+        SkASSERT(!isRT);
+        SkASSERT(0 == desc.fSampleCnt);
 
         if (!this->caps()->npotTextureTileSupport() &&
             (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
@@ -59,10 +99,10 @@
         }
 
         this->handleDirtyContext();
-        tex = this->onCreateCompressedTexture(desc, budgeted, srcData);
+        tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData);
     } else {
         this->handleDirtyContext();
-        tex = this->onCreateTexture(desc, budgeted, srcData, rowBytes);
+        tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes);
     }
     if (!this->caps()->reuseScratchTextures() && !isRT) {
         tex->resourcePriv().removeScratchKey();
diff --git a/src/gpu/GrGpu.h b/src/gpu/GrGpu.h
index 3df651f..5784781 100644
--- a/src/gpu/GrGpu.h
+++ b/src/gpu/GrGpu.h
@@ -437,9 +437,13 @@
     virtual void onResetContext(uint32_t resetBits) = 0;
 
     // overridden by backend-specific derived class to create objects.
-    virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted,
+    // Texture size and sample size will have already been validated in base class before
+    // onCreateTexture/CompressedTexture are called.
+    virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
+                                       GrGpuResource::LifeCycle lifeCycle,
                                        const void* srcData, size_t rowBytes) = 0;
-    virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
+    virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
+                                                 GrGpuResource::LifeCycle lifeCycle,
                                                  const void* srcData) = 0;
     virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
     virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
diff --git a/src/gpu/GrTest.cpp b/src/gpu/GrTest.cpp
index 09a221d..44be4bf 100644
--- a/src/gpu/GrTest.cpp
+++ b/src/gpu/GrTest.cpp
@@ -175,12 +175,12 @@
 private:
     void onResetContext(uint32_t resetBits) override {}
 
-    GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
-                               size_t rowBytes) override {
+    GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
+                               const void* srcData, size_t rowBytes) override {
         return NULL;
     }
 
-    GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
+    GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle,
                                          const void* srcData) override {
         return NULL;
     }
diff --git a/src/gpu/gl/GrGLGpu.cpp b/src/gpu/gl/GrGLGpu.cpp
index 954ecc4..dedd449 100644
--- a/src/gpu/gl/GrGLGpu.cpp
+++ b/src/gpu/gl/GrGLGpu.cpp
@@ -416,7 +416,8 @@
     GrGLTexture* texture = NULL;
     if (renderTarget) {
         GrGLRenderTarget::IDDesc rtIDDesc;
-        if (!this->createRenderTargetObjects(surfDesc, false, idDesc.fTextureID, &rtIDDesc)) {
+        if (!this->createRenderTargetObjects(surfDesc, GrGpuResource::kUncached_LifeCycle,
+                                             idDesc.fTextureID, &rtIDDesc)) {
             return NULL;
         }
         texture = SkNEW_ARGS(GrGLTextureRenderTarget, (this, surfDesc, idDesc, rtIDDesc));
@@ -811,13 +812,14 @@
     return (GR_GL_NO_ERROR == CHECK_ALLOC_ERROR(ctx.interface()));
 }
 
-bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc, bool budgeted, GrGLuint texID,
+bool GrGLGpu::createRenderTargetObjects(const GrSurfaceDesc& desc,
+                                        GrGpuResource::LifeCycle lifeCycle,
+                                        GrGLuint texID,
                                         GrGLRenderTarget::IDDesc* idDesc) {
     idDesc->fMSColorRenderbufferID = 0;
     idDesc->fRTFBOID = 0;
     idDesc->fTexFBOID = 0;
-    idDesc->fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
-                                    GrGpuResource::kUncached_LifeCycle;
+    idDesc->fLifeCycle = lifeCycle;
 
     GrGLenum status;
 
@@ -930,13 +932,9 @@
 }
 #endif
 
-GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& origDesc, bool budgeted,
+GrTexture* GrGLGpu::onCreateTexture(const GrSurfaceDesc& desc,
+                                    GrGpuResource::LifeCycle lifeCycle,
                                     const void* srcData, size_t rowBytes) {
-
-    GrSurfaceDesc desc = origDesc;
-
-    // Attempt to catch un- or wrongly initialized sample counts;
-    SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
     // We fail if the MSAA was requested and is not available.
     if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
         //SkDebugf("MSAA RT requested but not supported on this platform.");
@@ -945,31 +943,9 @@
 
     bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
 
-    // If the sample count exceeds the max then we clamp it.
-    desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
-    desc.fOrigin = resolve_origin(desc.fOrigin, renderTarget);
-
-    if (GrGLCaps::kNone_MSFBOType == this->glCaps().msFBOType() && desc.fSampleCnt) {
-        //SkDebugf("MSAA RT requested but not supported on this platform.");
-        return return_null_texture();
-    }
-
-    if (renderTarget) {
-        int maxRTSize = this->caps()->maxRenderTargetSize();
-        if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
-            return return_null_texture();
-        }
-    } else {
-        int maxSize = this->caps()->maxTextureSize();
-        if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
-            return return_null_texture();
-        }
-    }
-
     GrGLTexture::IDDesc idDesc;
     GL_CALL(GenTextures(1, &idDesc.fTextureID));
-    idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
-                                   GrGpuResource::kUncached_LifeCycle;
+    idDesc.fLifeCycle = lifeCycle;
 
     if (!idDesc.fTextureID) {
         return return_null_texture();
@@ -1020,7 +996,7 @@
         GL_CALL(BindTexture(GR_GL_TEXTURE_2D, 0));
         GrGLRenderTarget::IDDesc rtIDDesc;
 
-        if (!this->createRenderTargetObjects(desc, budgeted, idDesc.fTextureID, &rtIDDesc)) {
+        if (!this->createRenderTargetObjects(desc, lifeCycle, idDesc.fTextureID, &rtIDDesc)) {
             GL_CALL(DeleteTextures(1, &idDesc.fTextureID));
             return return_null_texture();
         }
@@ -1036,30 +1012,17 @@
     return tex;
 }
 
-GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& origDesc, bool budgeted,
+GrTexture* GrGLGpu::onCreateCompressedTexture(const GrSurfaceDesc& desc,
+                                              GrGpuResource::LifeCycle lifeCycle,
                                               const void* srcData) {
-
-    if(SkToBool(origDesc.fFlags & kRenderTarget_GrSurfaceFlag) || origDesc.fSampleCnt > 0) {
-        return return_null_texture();
-    }
-
     // Make sure that we're not flipping Y.
-    GrSurfaceOrigin texOrigin = resolve_origin(origDesc.fOrigin, false);
-    if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
-        return return_null_texture();
-    }
-    GrSurfaceDesc desc = origDesc;
-    desc.fOrigin = texOrigin;
-
-    int maxSize = this->caps()->maxTextureSize();
-    if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
+    if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
         return return_null_texture();
     }
 
     GrGLTexture::IDDesc idDesc;
     GL_CALL(GenTextures(1, &idDesc.fTextureID));
-    idDesc.fLifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
-                                   GrGpuResource::kUncached_LifeCycle;
+    idDesc.fLifeCycle = lifeCycle;
 
     if (!idDesc.fTextureID) {
         return return_null_texture();
diff --git a/src/gpu/gl/GrGLGpu.h b/src/gpu/gl/GrGLGpu.h
index 29d63fb..a8d8c2e 100644
--- a/src/gpu/gl/GrGLGpu.h
+++ b/src/gpu/gl/GrGLGpu.h
@@ -114,9 +114,10 @@
     // GrGpu overrides
     void onResetContext(uint32_t resetBits) override;
 
-    GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
-                               size_t rowBytes) override;
-    GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
+    GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
+                               const void* srcData, size_t rowBytes) override;
+    GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
+                                         GrGpuResource::LifeCycle lifeCycle,
                                          const void* srcData) override;
     GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override;
     GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override;
@@ -277,8 +278,8 @@
                                  int left = 0, int top = 0,
                                  int width = -1, int height = -1);
 
-    bool createRenderTargetObjects(const GrSurfaceDesc&, bool budgeted, GrGLuint texID, 
-                                   GrGLRenderTarget::IDDesc*);
+    bool createRenderTargetObjects(const GrSurfaceDesc&, GrGpuResource::LifeCycle lifeCycle,
+                                   GrGLuint texID, GrGLRenderTarget::IDDesc*);
 
     enum TempFBOTarget {
         kSrc_TempFBOTarget,