1// Copyright 2014 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#ifndef MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_BITMAP_UPLOADER_H_
6#define MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_BITMAP_UPLOADER_H_
7
8#include "base/callback.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "cc/surfaces/surface_id.h"
12#include "mojo/public/c/gles2/gles2.h"
13#include "mojo/services/public/cpp/view_manager/types.h"
14#include "mojo/services/public/interfaces/gpu/gpu.mojom.h"
15#include "mojo/services/public/interfaces/surfaces/surfaces.mojom.h"
16#include "mojo/services/public/interfaces/surfaces/surfaces_service.mojom.h"
17#include "third_party/skia/include/core/SkBitmap.h"
18#include "ui/gfx/geometry/size.h"
19
20namespace cc {
21class SurfaceIdAllocator;
22}
23
24namespace mojo {
25class ViewManagerClientImpl;
26
27class BitmapUploader : public SurfaceClient {
28 public:
29  BitmapUploader(ViewManagerClientImpl* client,
30                 Id view_id,
31                 SurfacesServicePtr surfaces_service,
32                 GpuPtr gpu_service);
33  virtual ~BitmapUploader();
34
35  void SetSize(const gfx::Size& size);
36  void SetColor(SkColor color);
37  void SetBitmap(SkBitmap bitmap);
38  void SetDoneCallback(const base::Callback<void(SurfaceIdPtr)>& done_callback);
39
40 private:
41  void Upload();
42  void OnSurfaceConnectionCreated(SurfacePtr surface, uint32_t id_namespace);
43  uint32_t BindTextureForSize(const gfx::Size size);
44
45  // SurfaceClient implementation.
46  virtual void ReturnResources(Array<ReturnedResourcePtr> resources) OVERRIDE;
47
48  ViewManagerClientImpl* client_;
49  Id view_id_;
50  SurfacesServicePtr surfaces_service_;
51  GpuPtr gpu_service_;
52  MojoGLES2Context gles2_context_;
53
54  gfx::Size size_;
55  SkColor color_;
56  SkBitmap bitmap_;
57  base::Callback<void(SurfaceIdPtr)> done_callback_;
58  SurfacePtr surface_;
59  cc::SurfaceId id_;
60  scoped_ptr<cc::SurfaceIdAllocator> id_allocator_;
61  gfx::Size surface_size_;
62  uint32_t next_resource_id_;
63  base::hash_map<uint32_t, uint32_t> resource_to_texture_id_map_;
64
65  base::WeakPtrFactory<BitmapUploader> weak_factory_;
66
67  DISALLOW_COPY_AND_ASSIGN(BitmapUploader);
68};
69
70}  // namespace mojo
71
72#endif  // MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_LIB_BITMAP_UPLOADER_H_
73