1// Copyright 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "cc/resources/scoped_resource.h"
6
7namespace cc {
8
9ScopedResource::ScopedResource(ResourceProvider* resource_provider)
10    : resource_provider_(resource_provider) {
11  DCHECK(resource_provider_);
12}
13
14ScopedResource::~ScopedResource() {
15  Free();
16}
17
18void ScopedResource::Allocate(const gfx::Size& size,
19                              ResourceProvider::TextureHint hint,
20                              ResourceFormat format) {
21  DCHECK(!id());
22  DCHECK(!size.IsEmpty());
23
24  set_dimensions(size, format);
25  set_id(resource_provider_->CreateResource(
26      size, GL_CLAMP_TO_EDGE, hint, format));
27
28#if DCHECK_IS_ON
29  allocate_thread_id_ = base::PlatformThread::CurrentId();
30#endif
31}
32
33void ScopedResource::AllocateManaged(const gfx::Size& size,
34                                     GLenum target,
35                                     ResourceFormat format) {
36  DCHECK(!id());
37  DCHECK(!size.IsEmpty());
38
39  set_dimensions(size, format);
40  set_id(resource_provider_->CreateManagedResource(
41      size,
42      target,
43      GL_CLAMP_TO_EDGE,
44      ResourceProvider::TextureHintImmutable,
45      format));
46
47#if DCHECK_IS_ON
48  allocate_thread_id_ = base::PlatformThread::CurrentId();
49#endif
50}
51
52void ScopedResource::Free() {
53  if (id()) {
54#if DCHECK_IS_ON
55    DCHECK(allocate_thread_id_ == base::PlatformThread::CurrentId());
56#endif
57    resource_provider_->DeleteResource(id());
58  }
59  set_id(0);
60}
61
62void ScopedResource::Leak() {
63  set_id(0);
64}
65
66}  // namespace cc
67