Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / cc / resources / resource_provider.h
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 #ifndef CC_RESOURCES_RESOURCE_PROVIDER_H_
6 #define CC_RESOURCES_RESOURCE_PROVIDER_H_
7
8 #include <deque>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 #include "base/basictypes.h"
15 #include "base/callback.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/memory/linked_ptr.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/threading/thread_checker.h"
20 #include "cc/base/cc_export.h"
21 #include "cc/output/context_provider.h"
22 #include "cc/output/output_surface.h"
23 #include "cc/resources/release_callback.h"
24 #include "cc/resources/resource_format.h"
25 #include "cc/resources/return_callback.h"
26 #include "cc/resources/shared_bitmap.h"
27 #include "cc/resources/single_release_callback.h"
28 #include "cc/resources/texture_mailbox.h"
29 #include "cc/resources/transferable_resource.h"
30 #include "third_party/khronos/GLES2/gl2.h"
31 #include "third_party/khronos/GLES2/gl2ext.h"
32 #include "third_party/skia/include/core/SkBitmap.h"
33 #include "third_party/skia/include/core/SkCanvas.h"
34 #include "ui/gfx/size.h"
35
36 class GrContext;
37
38 namespace gpu {
39 namespace gles {
40 class GLES2Interface;
41 }
42 }
43
44 namespace gfx {
45 class Rect;
46 class Vector2d;
47 }
48
49 namespace cc {
50 class IdAllocator;
51 class SharedBitmap;
52 class SharedBitmapManager;
53 class TextureUploader;
54
55 // This class is not thread-safe and can only be called from the thread it was
56 // created on (in practice, the impl thread).
57 class CC_EXPORT ResourceProvider {
58  public:
59   typedef unsigned ResourceId;
60   typedef std::vector<ResourceId> ResourceIdArray;
61   typedef std::set<ResourceId> ResourceIdSet;
62   typedef base::hash_map<ResourceId, ResourceId> ResourceIdMap;
63   enum TextureUsageHint {
64     TextureUsageAny,
65     TextureUsageFramebuffer,
66   };
67   enum ResourceType {
68     InvalidType = 0,
69     GLTexture = 1,
70     Bitmap,
71   };
72
73   static scoped_ptr<ResourceProvider> Create(
74       OutputSurface* output_surface,
75       SharedBitmapManager* shared_bitmap_manager,
76       int highp_threshold_min,
77       bool use_rgba_4444_texture_format,
78       size_t id_allocation_chunk_size,
79       bool use_distance_field_text);
80   virtual ~ResourceProvider();
81
82   void InitializeSoftware();
83   void InitializeGL();
84
85   void DidLoseOutputSurface() { lost_output_surface_ = true; }
86
87   int max_texture_size() const { return max_texture_size_; }
88   ResourceFormat memory_efficient_texture_format() const {
89     return use_rgba_4444_texture_format_ ? RGBA_4444 : best_texture_format_;
90   }
91   ResourceFormat best_texture_format() const { return best_texture_format_; }
92   bool use_sync_query() const { return use_sync_query_; }
93   size_t num_resources() const { return resources_.size(); }
94
95   // Checks whether a resource is in use by a consumer.
96   bool InUseByConsumer(ResourceId id);
97
98   bool IsLost(ResourceId id);
99   bool AllowOverlay(ResourceId id);
100
101   // Producer interface.
102
103   ResourceType default_resource_type() const { return default_resource_type_; }
104   ResourceType GetResourceType(ResourceId id);
105
106   // Creates a resource of the default resource type.
107   ResourceId CreateResource(const gfx::Size& size,
108                             GLint wrap_mode,
109                             TextureUsageHint hint,
110                             ResourceFormat format);
111
112   // Creates a resource which is tagged as being managed for GPU memory
113   // accounting purposes.
114   ResourceId CreateManagedResource(const gfx::Size& size,
115                                    GLenum target,
116                                    GLint wrap_mode,
117                                    TextureUsageHint hint,
118                                    ResourceFormat format);
119
120   // You can also explicitly create a specific resource type.
121   ResourceId CreateGLTexture(const gfx::Size& size,
122                              GLenum target,
123                              GLenum texture_pool,
124                              GLint wrap_mode,
125                              TextureUsageHint hint,
126                              ResourceFormat format);
127
128   ResourceId CreateBitmap(const gfx::Size& size, GLint wrap_mode);
129   // Wraps an IOSurface into a GL resource.
130   ResourceId CreateResourceFromIOSurface(const gfx::Size& size,
131                                          unsigned io_surface_id);
132
133   // Wraps an external texture mailbox into a GL resource.
134   ResourceId CreateResourceFromTextureMailbox(
135       const TextureMailbox& mailbox,
136       scoped_ptr<SingleReleaseCallback> release_callback);
137
138   void DeleteResource(ResourceId id);
139
140   // Update pixels from image, copying source_rect (in image) to dest_offset (in
141   // the resource).
142   void SetPixels(ResourceId id,
143                  const uint8_t* image,
144                  const gfx::Rect& image_rect,
145                  const gfx::Rect& source_rect,
146                  const gfx::Vector2d& dest_offset);
147
148   // Check upload status.
149   size_t NumBlockingUploads();
150   void MarkPendingUploadsAsNonBlocking();
151   size_t EstimatedUploadsPerTick();
152   void FlushUploads();
153   void ReleaseCachedData();
154   base::TimeTicks EstimatedUploadCompletionTime(size_t uploads_per_tick);
155
156   // Flush all context operations, kicking uploads and ensuring ordering with
157   // respect to other contexts.
158   void Flush();
159
160   // Finish all context operations, causing any pending callbacks to be
161   // scheduled.
162   void Finish();
163
164   // Only flush the command buffer if supported.
165   // Returns true if the shallow flush occurred, false otherwise.
166   bool ShallowFlushIfSupported();
167
168   // Creates accounting for a child. Returns a child ID.
169   int CreateChild(const ReturnCallback& return_callback);
170
171   // Destroys accounting for the child, deleting all accounted resources.
172   void DestroyChild(int child);
173
174   // Gets the child->parent resource ID map.
175   const ResourceIdMap& GetChildToParentMap(int child) const;
176
177   // Prepares resources to be transfered to the parent, moving them to
178   // mailboxes and serializing meta-data into TransferableResources.
179   // Resources are not removed from the ResourceProvider, but are marked as
180   // "in use".
181   void PrepareSendToParent(const ResourceIdArray& resources,
182                            TransferableResourceArray* transferable_resources);
183
184   // Receives resources from a child, moving them from mailboxes. Resource IDs
185   // passed are in the child namespace, and will be translated to the parent
186   // namespace, added to the child->parent map.
187   // This adds the resources to the working set in the ResourceProvider without
188   // declaring which resources are in use. Use DeclareUsedResourcesFromChild
189   // after calling this method to do that. All calls to ReceiveFromChild should
190   // be followed by a DeclareUsedResourcesFromChild.
191   // NOTE: if the sync_point is set on any TransferableResource, this will
192   // wait on it.
193   void ReceiveFromChild(
194       int child, const TransferableResourceArray& transferable_resources);
195
196   // Once a set of resources have been received, they may or may not be used.
197   // This declares what set of resources are currently in use from the child,
198   // releasing any other resources back to the child.
199   void DeclareUsedResourcesFromChild(
200       int child,
201       const ResourceIdArray& resources_from_child);
202
203   // Receives resources from the parent, moving them from mailboxes. Resource
204   // IDs passed are in the child namespace.
205   // NOTE: if the sync_point is set on any TransferableResource, this will
206   // wait on it.
207   void ReceiveReturnsFromParent(
208       const ReturnedResourceArray& transferable_resources);
209
210   // The following lock classes are part of the ResourceProvider API and are
211   // needed to read and write the resource contents. The user must ensure
212   // that they only use GL locks on GL resources, etc, and this is enforced
213   // by assertions.
214   class CC_EXPORT ScopedReadLockGL {
215    public:
216     ScopedReadLockGL(ResourceProvider* resource_provider,
217                      ResourceProvider::ResourceId resource_id);
218     virtual ~ScopedReadLockGL();
219
220     unsigned texture_id() const { return texture_id_; }
221
222    protected:
223     ResourceProvider* resource_provider_;
224     ResourceProvider::ResourceId resource_id_;
225
226    private:
227     unsigned texture_id_;
228
229     DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL);
230   };
231
232   class CC_EXPORT ScopedSamplerGL : public ScopedReadLockGL {
233    public:
234     ScopedSamplerGL(ResourceProvider* resource_provider,
235                     ResourceProvider::ResourceId resource_id,
236                     GLenum filter);
237     ScopedSamplerGL(ResourceProvider* resource_provider,
238                     ResourceProvider::ResourceId resource_id,
239                     GLenum unit,
240                     GLenum filter);
241     virtual ~ScopedSamplerGL();
242
243     GLenum target() const { return target_; }
244
245    private:
246     GLenum unit_;
247     GLenum target_;
248
249     DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL);
250   };
251
252   class CC_EXPORT ScopedWriteLockGL {
253    public:
254     ScopedWriteLockGL(ResourceProvider* resource_provider,
255                       ResourceProvider::ResourceId resource_id);
256     ~ScopedWriteLockGL();
257
258     unsigned texture_id() const { return texture_id_; }
259
260    private:
261     ResourceProvider* resource_provider_;
262     ResourceProvider::ResourceId resource_id_;
263     unsigned texture_id_;
264
265     DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL);
266   };
267
268   class CC_EXPORT ScopedReadLockSoftware {
269    public:
270     ScopedReadLockSoftware(ResourceProvider* resource_provider,
271                            ResourceProvider::ResourceId resource_id);
272     ~ScopedReadLockSoftware();
273
274     const SkBitmap* sk_bitmap() const {
275       DCHECK(valid());
276       return &sk_bitmap_;
277     }
278     GLint wrap_mode() const { return wrap_mode_; }
279
280     bool valid() const { return !!sk_bitmap_.getPixels(); }
281
282    private:
283     ResourceProvider* resource_provider_;
284     ResourceProvider::ResourceId resource_id_;
285     SkBitmap sk_bitmap_;
286     GLint wrap_mode_;
287
288     DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware);
289   };
290
291   class CC_EXPORT ScopedWriteLockSoftware {
292    public:
293     ScopedWriteLockSoftware(ResourceProvider* resource_provider,
294                             ResourceProvider::ResourceId resource_id);
295     ~ScopedWriteLockSoftware();
296
297     SkCanvas* sk_canvas() { return sk_canvas_.get(); }
298     bool valid() const { return !!sk_bitmap_.getPixels(); }
299
300    private:
301     ResourceProvider* resource_provider_;
302     ResourceProvider::ResourceId resource_id_;
303     SkBitmap sk_bitmap_;
304     scoped_ptr<SkCanvas> sk_canvas_;
305
306     DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware);
307   };
308
309   class Fence : public base::RefCounted<Fence> {
310    public:
311     Fence() {}
312     virtual bool HasPassed() = 0;
313
314    protected:
315     friend class base::RefCounted<Fence>;
316     virtual ~Fence() {}
317
318    private:
319     DISALLOW_COPY_AND_ASSIGN(Fence);
320   };
321
322   // Returns a canvas for direct rasterization.
323   // Call Unmap before the resource can be read or used for compositing.
324   // It is used for direct gpu rasterization.
325   SkCanvas* MapDirectRasterBuffer(ResourceId id);
326   void UnmapDirectRasterBuffer(ResourceId id);
327
328   // Returns a canvas backed by an image buffer. UnmapImageRasterBuffer
329   // returns true if canvas was written to while mapped.
330   // Rasterizing to the canvas writes the content into the image buffer,
331   // which is internally bound to the underlying resource when read.
332   // Call Unmap before the resource can be read or used for compositing.
333   // It is used by ImageRasterWorkerPool.
334   SkCanvas* MapImageRasterBuffer(ResourceId id);
335   bool UnmapImageRasterBuffer(ResourceId id);
336
337   // Returns a canvas backed by pixel buffer. UnmapPixelRasterBuffer
338   // returns true if canvas was written to while mapped.
339   // The pixel buffer needs to be uploaded to the underlying resource
340   // using BeginSetPixels before the resouce can be used for compositing.
341   // It is used by PixelRasterWorkerPool.
342   void AcquirePixelRasterBuffer(ResourceId id);
343   void ReleasePixelRasterBuffer(ResourceId id);
344   SkCanvas* MapPixelRasterBuffer(ResourceId id);
345   bool UnmapPixelRasterBuffer(ResourceId id);
346
347   // Asynchronously update pixels from acquired pixel buffer.
348   void BeginSetPixels(ResourceId id);
349   void ForceSetPixelsToComplete(ResourceId id);
350   bool DidSetPixelsComplete(ResourceId id);
351
352   // For tests only! This prevents detecting uninitialized reads.
353   // Use SetPixels or LockForWrite to allocate implicitly.
354   void AllocateForTesting(ResourceId id);
355
356   // For tests only!
357   void CreateForTesting(ResourceId id);
358
359   GLenum TargetForTesting(ResourceId id);
360
361   // Sets the current read fence. If a resource is locked for read
362   // and has read fences enabled, the resource will not allow writes
363   // until this fence has passed.
364   void SetReadLockFence(Fence* fence) { current_read_lock_fence_ = fence; }
365
366   // Enable read lock fences for a specific resource.
367   void EnableReadLockFences(ResourceProvider::ResourceId id, bool enable);
368
369   // Indicates if we can currently lock this resource for write.
370   bool CanLockForWrite(ResourceId id);
371
372   // Copy pixels from source to destination.
373   void CopyResource(ResourceId source_id, ResourceId dest_id);
374
375   static GLint GetActiveTextureUnit(gpu::gles2::GLES2Interface* gl);
376
377  private:
378   class DirectRasterBuffer;
379   class ImageRasterBuffer;
380   class PixelRasterBuffer;
381
382   struct Resource {
383     enum Origin { Internal, External, Delegated };
384
385     Resource();
386     ~Resource();
387     Resource(unsigned texture_id,
388              const gfx::Size& size,
389              Origin origin,
390              GLenum target,
391              GLenum filter,
392              GLenum texture_pool,
393              GLint wrap_mode,
394              TextureUsageHint hint,
395              ResourceFormat format);
396     Resource(uint8_t* pixels,
397              SharedBitmap* bitmap,
398              const gfx::Size& size,
399              Origin origin,
400              GLenum filter,
401              GLint wrap_mode);
402     Resource(const SharedBitmapId& bitmap_id,
403              const gfx::Size& size,
404              Origin origin,
405              GLenum filter,
406              GLint wrap_mode);
407
408     int child_id;
409     unsigned gl_id;
410     // Pixel buffer used for set pixels without unnecessary copying.
411     unsigned gl_pixel_buffer_id;
412     // Query used to determine when asynchronous set pixels complete.
413     unsigned gl_upload_query_id;
414     // Query used to determine when read lock fence has passed.
415     unsigned gl_read_lock_query_id;
416     TextureMailbox mailbox;
417     ReleaseCallback release_callback;
418     uint8_t* pixels;
419     int lock_for_read_count;
420     int imported_count;
421     int exported_count;
422     bool dirty_image : 1;
423     bool locked_for_write : 1;
424     bool lost : 1;
425     bool marked_for_deletion : 1;
426     bool pending_set_pixels : 1;
427     bool set_pixels_completion_forced : 1;
428     bool allocated : 1;
429     bool enable_read_lock_fences : 1;
430     bool has_shared_bitmap_id : 1;
431     bool allow_overlay : 1;
432     scoped_refptr<Fence> read_lock_fence;
433     gfx::Size size;
434     Origin origin;
435     GLenum target;
436     // TODO(skyostil): Use a separate sampler object for filter state.
437     GLenum original_filter;
438     GLenum filter;
439     unsigned image_id;
440     unsigned bound_image_id;
441     GLenum texture_pool;
442     GLint wrap_mode;
443     TextureUsageHint hint;
444     ResourceType type;
445     ResourceFormat format;
446     SharedBitmapId shared_bitmap_id;
447     SharedBitmap* shared_bitmap;
448     linked_ptr<DirectRasterBuffer> direct_raster_buffer;
449     linked_ptr<ImageRasterBuffer> image_raster_buffer;
450     linked_ptr<PixelRasterBuffer> pixel_raster_buffer;
451   };
452   typedef base::hash_map<ResourceId, Resource> ResourceMap;
453
454   class RasterBuffer {
455    public:
456     virtual ~RasterBuffer();
457
458     SkCanvas* LockForWrite();
459     // Returns true if canvas was written to while locked.
460     bool UnlockForWrite();
461
462    protected:
463     RasterBuffer(const Resource* resource, ResourceProvider* resource_provider);
464     const Resource* resource() const { return resource_; }
465     ResourceProvider* resource_provider() const { return resource_provider_; }
466
467     virtual SkCanvas* DoLockForWrite() = 0;
468     virtual bool DoUnlockForWrite() = 0;
469
470    private:
471     const Resource* resource_;
472     ResourceProvider* resource_provider_;
473     SkCanvas* locked_canvas_;
474     int canvas_save_count_;
475   };
476
477   class DirectRasterBuffer : public RasterBuffer {
478    public:
479     DirectRasterBuffer(const Resource* resource,
480                        ResourceProvider* resource_provider,
481                        bool use_distance_field_text);
482     virtual ~DirectRasterBuffer();
483
484    protected:
485     virtual SkCanvas* DoLockForWrite() OVERRIDE;
486     virtual bool DoUnlockForWrite() OVERRIDE;
487     skia::RefPtr<SkSurface> CreateSurface();
488
489    private:
490     skia::RefPtr<SkSurface> surface_;
491     uint32_t surface_generation_id_;
492     const bool use_distance_field_text_;
493
494     DISALLOW_COPY_AND_ASSIGN(DirectRasterBuffer);
495   };
496
497   class BitmapRasterBuffer : public RasterBuffer {
498    public:
499     virtual ~BitmapRasterBuffer();
500
501    protected:
502     BitmapRasterBuffer(const Resource* resource,
503                        ResourceProvider* resource_provider);
504
505     virtual SkCanvas* DoLockForWrite() OVERRIDE;
506     virtual bool DoUnlockForWrite() OVERRIDE;
507
508     virtual uint8_t* MapBuffer(int* stride) = 0;
509     virtual void UnmapBuffer() = 0;
510
511    private:
512     uint8_t* mapped_buffer_;
513     SkBitmap raster_bitmap_;
514     uint32_t raster_bitmap_generation_id_;
515     skia::RefPtr<SkCanvas> raster_canvas_;
516   };
517
518   class ImageRasterBuffer : public BitmapRasterBuffer {
519    public:
520     ImageRasterBuffer(const Resource* resource,
521                       ResourceProvider* resource_provider);
522     virtual ~ImageRasterBuffer();
523
524    protected:
525     virtual uint8_t* MapBuffer(int* stride) OVERRIDE;
526     virtual void UnmapBuffer() OVERRIDE;
527
528    private:
529     DISALLOW_COPY_AND_ASSIGN(ImageRasterBuffer);
530   };
531
532   class PixelRasterBuffer : public BitmapRasterBuffer {
533    public:
534     PixelRasterBuffer(const Resource* resource,
535                       ResourceProvider* resource_provider);
536     virtual ~PixelRasterBuffer();
537
538    protected:
539     virtual uint8_t* MapBuffer(int* stride) OVERRIDE;
540     virtual void UnmapBuffer() OVERRIDE;
541
542    private:
543     DISALLOW_COPY_AND_ASSIGN(PixelRasterBuffer);
544   };
545
546   static bool CompareResourceMapIteratorsByChildId(
547       const std::pair<ReturnedResource, ResourceMap::iterator>& a,
548       const std::pair<ReturnedResource, ResourceMap::iterator>& b);
549
550   struct Child {
551     Child();
552     ~Child();
553
554     ResourceIdMap child_to_parent_map;
555     ResourceIdMap parent_to_child_map;
556     ReturnCallback return_callback;
557     ResourceIdSet in_use_resources;
558     bool marked_for_deletion;
559   };
560   typedef base::hash_map<int, Child> ChildMap;
561
562   bool ReadLockFenceHasPassed(const Resource* resource) {
563     return !resource->read_lock_fence.get() ||
564            resource->read_lock_fence->HasPassed();
565   }
566
567   ResourceProvider(OutputSurface* output_surface,
568                    SharedBitmapManager* shared_bitmap_manager,
569                    int highp_threshold_min,
570                    bool use_rgba_4444_texture_format,
571                    size_t id_allocation_chunk_size,
572                    bool use_distance_field_text);
573
574   void CleanUpGLIfNeeded();
575
576   Resource* GetResource(ResourceId id);
577   const Resource* LockForRead(ResourceId id);
578   void UnlockForRead(ResourceId id);
579   const Resource* LockForWrite(ResourceId id);
580   void UnlockForWrite(ResourceId id);
581   static void PopulateSkBitmapWithResource(SkBitmap* sk_bitmap,
582                                            const Resource* resource);
583
584   void TransferResource(gpu::gles2::GLES2Interface* gl,
585                         ResourceId id,
586                         TransferableResource* resource);
587   enum DeleteStyle {
588     Normal,
589     ForShutdown,
590   };
591   void DeleteResourceInternal(ResourceMap::iterator it, DeleteStyle style);
592   void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it,
593                                              DeleteStyle style,
594                                              const ResourceIdArray& unused);
595   void DestroyChildInternal(ChildMap::iterator it, DeleteStyle style);
596   void LazyCreate(Resource* resource);
597   void LazyAllocate(Resource* resource);
598
599   // TODO(alokp): Move the implementation to PixelRasterBuffer.
600   // Acquire pixel buffer for resource. The pixel buffer can be used to
601   // set resource pixels without performing unnecessary copying.
602   void AcquirePixelBuffer(Resource* resource);
603   void ReleasePixelBuffer(Resource* resource);
604   // Map/unmap the acquired pixel buffer.
605   uint8_t* MapPixelBuffer(const Resource* resource, int* stride);
606   void UnmapPixelBuffer(const Resource* resource);
607
608   // TODO(alokp): Move the implementation to ImageRasterBuffer.
609   // Acquire and release an image. The image allows direct
610   // manipulation of texture memory.
611   void AcquireImage(Resource* resource);
612   void ReleaseImage(Resource* resource);
613   // Maps the acquired image so that its pixels could be modified.
614   // Unmap is called when all pixels are set.
615   uint8_t* MapImage(const Resource* resource, int* stride);
616   void UnmapImage(const Resource* resource);
617
618   void BindImageForSampling(Resource* resource);
619   // Binds the given GL resource to a texture target for sampling using the
620   // specified filter for both minification and magnification. Returns the
621   // texture target used. The resource must be locked for reading.
622   GLenum BindForSampling(ResourceProvider::ResourceId resource_id,
623                          GLenum unit,
624                          GLenum filter);
625
626   // Returns NULL if the output_surface_ does not have a ContextProvider.
627   gpu::gles2::GLES2Interface* ContextGL() const;
628   class GrContext* GrContext() const;
629
630   OutputSurface* output_surface_;
631   SharedBitmapManager* shared_bitmap_manager_;
632   bool lost_output_surface_;
633   int highp_threshold_min_;
634   ResourceId next_id_;
635   ResourceMap resources_;
636   int next_child_;
637   ChildMap children_;
638
639   ResourceType default_resource_type_;
640   bool use_texture_storage_ext_;
641   bool use_texture_usage_hint_;
642   bool use_compressed_texture_etc1_;
643   scoped_ptr<TextureUploader> texture_uploader_;
644   int max_texture_size_;
645   ResourceFormat best_texture_format_;
646
647   base::ThreadChecker thread_checker_;
648
649   scoped_refptr<Fence> current_read_lock_fence_;
650   bool use_rgba_4444_texture_format_;
651
652   const size_t id_allocation_chunk_size_;
653   scoped_ptr<IdAllocator> texture_id_allocator_;
654   scoped_ptr<IdAllocator> buffer_id_allocator_;
655
656   bool use_sync_query_;
657
658   bool use_distance_field_text_;
659
660   DISALLOW_COPY_AND_ASSIGN(ResourceProvider);
661 };
662
663
664 // TODO(epenner): Move these format conversions to resource_format.h
665 // once that builds on mac (npapi.h currently #includes OpenGL.h).
666 inline unsigned BitsPerPixel(ResourceFormat format) {
667   DCHECK_LE(format, RESOURCE_FORMAT_MAX);
668   static const unsigned format_bits_per_pixel[RESOURCE_FORMAT_MAX + 1] = {
669     32,  // RGBA_8888
670     16,  // RGBA_4444
671     32,  // BGRA_8888
672     8,   // LUMINANCE_8
673     16,  // RGB_565,
674     4    // ETC1
675   };
676   return format_bits_per_pixel[format];
677 }
678
679 inline GLenum GLDataType(ResourceFormat format) {
680   DCHECK_LE(format, RESOURCE_FORMAT_MAX);
681   static const unsigned format_gl_data_type[RESOURCE_FORMAT_MAX + 1] = {
682     GL_UNSIGNED_BYTE,           // RGBA_8888
683     GL_UNSIGNED_SHORT_4_4_4_4,  // RGBA_4444
684     GL_UNSIGNED_BYTE,           // BGRA_8888
685     GL_UNSIGNED_BYTE,           // LUMINANCE_8
686     GL_UNSIGNED_SHORT_5_6_5,    // RGB_565,
687     GL_UNSIGNED_BYTE            // ETC1
688   };
689   return format_gl_data_type[format];
690 }
691
692 inline GLenum GLDataFormat(ResourceFormat format) {
693   DCHECK_LE(format, RESOURCE_FORMAT_MAX);
694   static const unsigned format_gl_data_format[RESOURCE_FORMAT_MAX + 1] = {
695     GL_RGBA,           // RGBA_8888
696     GL_RGBA,           // RGBA_4444
697     GL_BGRA_EXT,       // BGRA_8888
698     GL_LUMINANCE,      // LUMINANCE_8
699     GL_RGB,            // RGB_565
700     GL_ETC1_RGB8_OES   // ETC1
701   };
702   return format_gl_data_format[format];
703 }
704
705 inline GLenum GLInternalFormat(ResourceFormat format) {
706   return GLDataFormat(format);
707 }
708
709 }  // namespace cc
710
711 #endif  // CC_RESOURCES_RESOURCE_PROVIDER_H_