13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// found in the LICENSE file.
43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
50f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "cc/test/test_context_provider.h"
63551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include <set>
8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include <vector>
9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/bind.h"
113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/callback_helpers.h"
123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/logging.h"
13a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "cc/test/test_gles2_interface.h"
140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "cc/test/test_web_graphics_context_3d.h"
153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)namespace cc {
173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// static
193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)scoped_refptr<TestContextProvider> TestContextProvider::Create() {
203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return Create(TestWebGraphicsContext3D::Create().Pass());
213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// static
243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)scoped_refptr<TestContextProvider> TestContextProvider::Create(
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    scoped_ptr<TestWebGraphicsContext3D> context) {
264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!context)
274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return NULL;
284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return new TestContextProvider(context.Pass());
293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TestContextProvider::TestContextProvider(
324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    scoped_ptr<TestWebGraphicsContext3D> context)
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : context3d_(context.Pass()),
34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      context_gl_(new TestGLES2Interface(context3d_.get())),
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      bound_(false),
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      destroyed_(false),
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      weak_ptr_factory_(this) {
383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(main_thread_checker_.CalledOnValidThread());
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DCHECK(context3d_);
403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  context_thread_checker_.DetachFromThread();
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  context3d_->set_test_support(&support_);
423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)TestContextProvider::~TestContextProvider() {
453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(main_thread_checker_.CalledOnValidThread() ||
463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)         context_thread_checker_.CalledOnValidThread());
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool TestContextProvider::BindToCurrentThread() {
503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // This is called on the thread the context will be used.
513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (bound_)
543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return true;
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
56a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (context3d_->isContextLost()) {
573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    base::AutoLock lock(destroyed_lock_);
583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    destroyed_ = true;
593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return false;
603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
61a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bound_ = true;
623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  context3d_->set_context_lost_callback(
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::Bind(&TestContextProvider::OnLostContext,
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                 base::Unretained(this)));
663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return true;
683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
70424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)ContextProvider::Capabilities TestContextProvider::ContextCapabilities() {
71424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(bound_);
72424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return context3d_->test_capabilities();
75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
77a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() {
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(context3d_);
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(bound_);
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
82a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return context_gl_.get();
83a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
84a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
85a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)gpu::ContextSupport* TestContextProvider::ContextSupport() {
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return &support_;
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class GrContext* TestContextProvider::GrContext() {
903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(bound_);
913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // TODO(danakj): Make a test GrContext that works with a test Context3d.
943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return NULL;
953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool TestContextProvider::IsContextLost() {
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(bound_);
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return context3d_->isContextLost();
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void TestContextProvider::VerifyContexts() {
1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(bound_);
1063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
1073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (context3d_->isContextLost()) {
1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    base::AutoLock lock(destroyed_lock_);
1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    destroyed_ = true;
1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid TestContextProvider::DeleteCachedResources() {
1155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
1165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
1173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool TestContextProvider::DestroyedOnMainThread() {
1183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(main_thread_checker_.CalledOnValidThread());
1193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::AutoLock lock(destroyed_lock_);
1213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return destroyed_;
1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void TestContextProvider::OnLostContext() {
1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  {
1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    base::AutoLock lock(destroyed_lock_);
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (destroyed_)
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      return;
1303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    destroyed_ = true;
1313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (!lost_context_callback_.is_null())
1333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    base::ResetAndReturn(&lost_context_callback_).Run();
1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)TestWebGraphicsContext3D* TestContextProvider::TestContext3d() {
1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(bound_);
1383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
1393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return context3d_.get();
1413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() {
1443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return context3d_.get();
1453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void TestContextProvider::SetMemoryAllocation(
1480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    const ManagedMemoryPolicy& policy) {
1493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (memory_policy_changed_callback_.is_null())
1503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return;
1510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  memory_policy_changed_callback_.Run(policy);
1523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void TestContextProvider::SetLostContextCallback(
1553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const LostContextCallback& cb) {
1563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
1573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(lost_context_callback_.is_null() || cb.is_null());
1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  lost_context_callback_ = cb;
1593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void TestContextProvider::SetMemoryPolicyChangedCallback(
1623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const MemoryPolicyChangedCallback& cb) {
1633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(context_thread_checker_.CalledOnValidThread());
1643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null());
1653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  memory_policy_changed_callback_ = cb;
1663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
16858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TestContextProvider::SetMaxTransferBufferUsageBytes(
16958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    size_t max_transfer_buffer_usage_bytes) {
17058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  context3d_->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes);
17158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
17258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}  // namespace cc
174