1/*
2 * Copyright 2011 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#include "GrGLTexture.h"
9#include "GrGLGpu.h"
10#include "SkTraceMemoryDump.h"
11
12#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
13#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
14
15// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
16GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc)
17    : GrSurface(gpu, idDesc.fLifeCycle, desc)
18    , INHERITED(gpu, idDesc.fLifeCycle, desc) {
19    this->init(desc, idDesc);
20    this->registerWithCache();
21}
22
23GrGLTexture::GrGLTexture(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc, Derived)
24    : GrSurface(gpu, idDesc.fLifeCycle, desc)
25    , INHERITED(gpu, idDesc.fLifeCycle, desc) {
26    this->init(desc, idDesc);
27}
28
29void GrGLTexture::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
30    SkASSERT(0 != idDesc.fInfo.fID);
31    fTexParams.invalidate();
32    fTexParamsTimestamp = GrGpu::kExpiredTimestamp;
33    fInfo = idDesc.fInfo;
34    fTextureIDLifecycle = idDesc.fLifeCycle;
35}
36
37void GrGLTexture::onRelease() {
38    if (fInfo.fID) {
39        if (GrGpuResource::kBorrowed_LifeCycle != fTextureIDLifecycle) {
40            if (this->desc().fTextureStorageAllocator.fDeallocateTextureStorage) {
41                this->desc().fTextureStorageAllocator.fDeallocateTextureStorage(
42                        this->desc().fTextureStorageAllocator.fCtx,
43                        reinterpret_cast<GrBackendObject>(&fInfo));
44            } else {
45                GL_CALL(DeleteTextures(1, &fInfo.fID));
46            }
47        }
48        fInfo.fID = 0;
49    }
50    INHERITED::onRelease();
51}
52
53void GrGLTexture::onAbandon() {
54    fInfo.fTarget = 0;
55    fInfo.fID = 0;
56    INHERITED::onAbandon();
57}
58
59GrBackendObject GrGLTexture::getTextureHandle() const {
60#ifdef SK_IGNORE_GL_TEXTURE_TARGET
61    return static_cast<GrBackendObject>(this->textureID());
62#else
63    return reinterpret_cast<GrBackendObject>(&fInfo);
64#endif
65}
66
67void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
68                                   const SkString& dumpName) const {
69    SkString texture_id;
70    texture_id.appendU32(this->textureID());
71    traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_texture",
72                                      texture_id.c_str());
73}
74