Home | History | Annotate | Download | only in service
      1 // Copyright (c) 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 "gpu/command_buffer/service/image_manager.h"
      6 
      7 #include "ui/gl/gl_image.h"
      8 
      9 namespace gpu {
     10 namespace gles2 {
     11 
     12 ImageManager::ImageManager() : release_after_use_(false) {
     13 }
     14 
     15 ImageManager::~ImageManager() {
     16 }
     17 
     18 bool ImageManager::RegisterGpuMemoryBuffer(int32 id,
     19                                            gfx::GpuMemoryBufferHandle buffer,
     20                                            size_t width,
     21                                            size_t height,
     22                                            unsigned internalformat) {
     23   if (id <= 0) {
     24     DVLOG(0) << "Cannot register GPU memory buffer with non-positive ID.";
     25     return false;
     26   }
     27 
     28   if (LookupImage(id)) {
     29     DVLOG(0) << "GPU memory buffer ID already in use.";
     30     return false;
     31   }
     32 
     33   scoped_refptr<gfx::GLImage> gl_image =
     34       gfx::GLImage::CreateGLImageForGpuMemoryBuffer(buffer,
     35                                                     gfx::Size(width, height),
     36                                                     internalformat);
     37   if (!gl_image)
     38     return false;
     39 
     40   if (release_after_use_)
     41     gl_image->SetReleaseAfterUse();
     42 
     43   AddImage(gl_image.get(), id);
     44   return true;
     45 }
     46 
     47 void ImageManager::DestroyGpuMemoryBuffer(int32 id) {
     48   RemoveImage(id);
     49 }
     50 
     51 void ImageManager::AddImage(gfx::GLImage* image, int32 service_id) {
     52   gl_images_[service_id] = image;
     53 }
     54 
     55 void ImageManager::RemoveImage(int32 service_id) {
     56   gl_images_.erase(service_id);
     57 }
     58 
     59 gfx::GLImage* ImageManager::LookupImage(int32 service_id) {
     60   GLImageMap::const_iterator iter = gl_images_.find(service_id);
     61   if (iter != gl_images_.end())
     62     return iter->second.get();
     63 
     64   return NULL;
     65 }
     66 
     67 void ImageManager::SetReleaseAfterUse() {
     68   release_after_use_ = true;
     69 }
     70 
     71 }  // namespace gles2
     72 }  // namespace gpu
     73