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