summaryrefslogtreecommitdiffstats
path: root/cc/scoped_resource.cc
blob: c5894aaac2fff829c071b0b85606ae8107425c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/scoped_resource.h"

namespace cc {

ScopedResource::ScopedResource(ResourceProvider* resource_provider)
    : resource_provider_(resource_provider) {
  DCHECK(resource_provider_);
}

ScopedResource::~ScopedResource() {
  Free();
}

bool ScopedResource::Allocate(const gfx::Size& size, GLenum format,
                              ResourceProvider::TextureUsageHint hint) {
  DCHECK(!id());
  DCHECK(!size.IsEmpty());

  set_dimensions(size, format);
  set_id(resource_provider_->createResource(size, format, hint));

#ifndef NDEBUG
  allocate_thread_id_ = base::PlatformThread::CurrentId();
#endif

  return id();
}

void ScopedResource::Free() {
  if (id()) {
#ifndef NDEBUG
    DCHECK(allocate_thread_id_ == base::PlatformThread::CurrentId());
#endif
    resource_provider_->deleteResource(id());
  }
  set_id(0);
}

void ScopedResource::Leak() {
  set_id(0);
}

}  // namespace cc