Home | History | Annotate | Download | only in test
      1 // Copyright 2013 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/test/fake_context_provider.h"
      6 
      7 #include "cc/test/test_web_graphics_context_3d.h"
      8 
      9 namespace cc {
     10 
     11 FakeContextProvider::FakeContextProvider()
     12     : bound_(false),
     13       destroyed_(false) {
     14   DCHECK(main_thread_checker_.CalledOnValidThread());
     15   context_thread_checker_.DetachFromThread();
     16 }
     17 
     18 FakeContextProvider::~FakeContextProvider() {
     19   DCHECK(main_thread_checker_.CalledOnValidThread() ||
     20          context_thread_checker_.CalledOnValidThread());
     21 }
     22 
     23 bool FakeContextProvider::InitializeOnMainThread(
     24     const CreateCallback& create_callback) {
     25   DCHECK(main_thread_checker_.CalledOnValidThread());
     26 
     27   DCHECK(!context3d_);
     28   if (create_callback.is_null())
     29     context3d_ = TestWebGraphicsContext3D::Create().Pass();
     30   else
     31     context3d_ = create_callback.Run();
     32   return context3d_;
     33 }
     34 
     35 bool FakeContextProvider::BindToCurrentThread() {
     36   DCHECK(context3d_);
     37 
     38   // This is called on the thread the context will be used.
     39   DCHECK(context_thread_checker_.CalledOnValidThread());
     40 
     41   if (bound_)
     42     return true;
     43 
     44   bound_ = true;
     45   if (!context3d_->makeContextCurrent()) {
     46     base::AutoLock lock(destroyed_lock_);
     47     destroyed_ = true;
     48     return false;
     49   }
     50   return true;
     51 }
     52 
     53 WebKit::WebGraphicsContext3D* FakeContextProvider::Context3d() {
     54   DCHECK(context3d_);
     55   DCHECK(bound_);
     56   DCHECK(context_thread_checker_.CalledOnValidThread());
     57 
     58   return context3d_.get();
     59 }
     60 class GrContext* FakeContextProvider::GrContext() {
     61   DCHECK(context3d_);
     62   DCHECK(bound_);
     63   DCHECK(context_thread_checker_.CalledOnValidThread());
     64 
     65   // TODO(danakj): Make a fake GrContext.
     66   return NULL;
     67 }
     68 
     69 void FakeContextProvider::VerifyContexts() {
     70   DCHECK(context3d_);
     71   DCHECK(bound_);
     72   DCHECK(context_thread_checker_.CalledOnValidThread());
     73 
     74   if (context3d_->isContextLost()) {
     75     base::AutoLock lock(destroyed_lock_);
     76     destroyed_ = true;
     77   }
     78 }
     79 
     80 bool FakeContextProvider::DestroyedOnMainThread() {
     81   DCHECK(main_thread_checker_.CalledOnValidThread());
     82 
     83   base::AutoLock lock(destroyed_lock_);
     84   return destroyed_;
     85 }
     86 
     87 void FakeContextProvider::SetLostContextCallback(
     88     const LostContextCallback& cb) {
     89   DCHECK(context_thread_checker_.CalledOnValidThread());
     90   NOTIMPLEMENTED();
     91 }
     92 
     93 }  // namespace cc
     94