Upstream version 9.38.198.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   // Only flush the command buffer if supported.
157   // Returns true if the shallow flush occurred, false otherwise.
158   bool ShallowFlushIfSupported();
159
160   // Creates accounting for a child. Returns a child ID.
161   int CreateChild(const ReturnCallback& return_callback);
162
163   // Destroys accounting for the child, deleting all accounted resources.
164   void DestroyChild(int child);
165
166   // Gets the child->parent resource ID map.
167   const ResourceIdMap& GetChildToParentMap(int child) const;
168
169   // Prepares resources to be transfered to the parent, moving them to
170   // mailboxes and serializing meta-data into TransferableResources.
171   // Resources are not removed from the ResourceProvider, but are marked as
172   // "in use".
173   void PrepareSendToParent(const ResourceIdArray& resources,
174                            TransferableResourceArray* transferable_resources);
175
176   // Receives resources from a child, moving them from mailboxes. Resource IDs
177   // passed are in the child namespace, and will be translated to the parent
178   // namespace, added to the child->parent map.
179   // This adds the resources to the working set in the ResourceProvider without
180   // declaring which resources are in use. Use DeclareUsedResourcesFromChild
181   // after calling this method to do that. All calls to ReceiveFromChild should
182   // be followed by a DeclareUsedResourcesFromChild.
183   // NOTE: if the sync_point is set on any TransferableResource, this will
184   // wait on it.
185   void ReceiveFromChild(
186       int child, const TransferableResourceArray& transferable_resources);
187
188   // Once a set of resources have been received, they may or may not be used.
189   // This declares what set of resources are currently in use from the child,
190   // releasing any other resources back to the child.
191   void DeclareUsedResourcesFromChild(
192       int child,
193       const ResourceIdArray& resources_from_child);
194
195   // Receives resources from the parent, moving them from mailboxes. Resource
196   // IDs passed are in the child namespace.
197   // NOTE: if the sync_point is set on any TransferableResource, this will
198   // wait on it.
199   void ReceiveReturnsFromParent(
200       const ReturnedResourceArray& transferable_resources);
201
202   // The following lock classes are part of the ResourceProvider API and are
203   // needed to read and write the resource contents. The user must ensure
204   // that they only use GL locks on GL resources, etc, and this is enforced
205   // by assertions.
206   class CC_EXPORT ScopedReadLockGL {
207    public:
208     ScopedReadLockGL(ResourceProvider* resource_provider,
209                      ResourceProvider::ResourceId resource_id);
210     virtual ~ScopedReadLockGL();
211
212     unsigned texture_id() const { return texture_id_; }
213
214    protected:
215     ResourceProvider* resource_provider_;
216     ResourceProvider::ResourceId resource_id_;
217
218    private:
219     unsigned texture_id_;
220
221     DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL);
222   };
223
224   class CC_EXPORT ScopedSamplerGL : public ScopedReadLockGL {
225    public:
226     ScopedSamplerGL(ResourceProvider* resource_provider,
227                     ResourceProvider::ResourceId resource_id,
228                     GLenum filter);
229     ScopedSamplerGL(ResourceProvider* resource_provider,
230                     ResourceProvider::ResourceId resource_id,
231                     GLenum unit,
232                     GLenum filter);
233     virtual ~ScopedSamplerGL();
234
235     GLenum target() const { return target_; }
236
237    private:
238     GLenum unit_;
239     GLenum target_;
240
241     DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL);
242   };
243
244   class CC_EXPORT ScopedWriteLockGL {
245    public:
246     ScopedWriteLockGL(ResourceProvider* resource_provider,
247                       ResourceProvider::ResourceId resource_id);
248     ~ScopedWriteLockGL();
249
250     unsigned texture_id() const { return texture_id_; }
251
252    private:
253     ResourceProvider* resource_provider_;
254     ResourceProvider::ResourceId resource_id_;
255     unsigned texture_id_;
256
257     DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL);
258   };
259
260   class CC_EXPORT ScopedReadLockSoftware {
261    public:
262     ScopedReadLockSoftware(ResourceProvider* resource_provider,
263                            ResourceProvider::ResourceId resource_id);
264     ~ScopedReadLockSoftware();
265
266     const SkBitmap* sk_bitmap() const {
267       DCHECK(valid());
268       return &sk_bitmap_;
269     }
270     GLint wrap_mode() const { return wrap_mode_; }
271
272     bool valid() const { return !!sk_bitmap_.getPixels(); }
273
274    private:
275     ResourceProvider* resource_provider_;
276     ResourceProvider::ResourceId resource_id_;
277     SkBitmap sk_bitmap_;
278     GLint wrap_mode_;
279
280     DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware);
281   };
282
283   class CC_EXPORT ScopedWriteLockSoftware {
284    public:
285     ScopedWriteLockSoftware(ResourceProvider* resource_provider,
286                             ResourceProvider::ResourceId resource_id);
287     ~ScopedWriteLockSoftware();
288
289     SkCanvas* sk_canvas() { return sk_canvas_.get(); }
290     bool valid() const { return !!sk_bitmap_.getPixels(); }
291
292    private:
293     ResourceProvider* resource_provider_;
294     ResourceProvider::ResourceId resource_id_;
295     SkBitmap sk_bitmap_;
296     scoped_ptr<SkCanvas> sk_canvas_;
297
298     DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware);
299   };
300
301   class Fence : public base::RefCounted<Fence> {
302    public:
303     Fence() {}
304
305     virtual void Set() = 0;
306     virtual bool HasPassed() = 0;
307
308    protected:
309     friend class base::RefCounted<Fence>;
310     virtual ~Fence() {}
311
312    private:
313     DISALLOW_COPY_AND_ASSIGN(Fence);
314   };
315
316   // Returns a canvas for gpu rasterization.
317   // Call Unmap before the resource can be read or used for compositing.
318   // It is used for direct gpu rasterization.
319   SkCanvas* MapGpuRasterBuffer(ResourceId id);
320   void UnmapGpuRasterBuffer(ResourceId id);
321
322   // Returns a canvas backed by an image buffer. UnmapImageRasterBuffer
323   // returns true if canvas was written to while mapped.
324   // Rasterizing to the canvas writes the content into the image buffer,
325   // which is internally bound to the underlying resource when read.
326   // Call Unmap before the resource can be read or used for compositing.
327   // It is used by ImageRasterWorkerPool.
328   SkCanvas* MapImageRasterBuffer(ResourceId id);
329   bool UnmapImageRasterBuffer(ResourceId id);
330
331   // Returns a canvas backed by pixel buffer. UnmapPixelRasterBuffer
332   // returns true if canvas was written to while mapped.
333   // The pixel buffer needs to be uploaded to the underlying resource
334   // using BeginSetPixels before the resouce can be used for compositing.
335   // It is used by PixelRasterWorkerPool.
336   void AcquirePixelRasterBuffer(ResourceId id);
337   void ReleasePixelRasterBuffer(ResourceId id);
338   SkCanvas* MapPixelRasterBuffer(ResourceId id);
339   bool UnmapPixelRasterBuffer(ResourceId id);
340
341   // Asynchronously update pixels from acquired pixel buffer.
342   void BeginSetPixels(ResourceId id);
343   void ForceSetPixelsToComplete(ResourceId id);
344   bool DidSetPixelsComplete(ResourceId id);
345
346   // For tests only! This prevents detecting uninitialized reads.
347   // Use SetPixels or LockForWrite to allocate implicitly.
348   void AllocateForTesting(ResourceId id);
349
350   // For tests only!
351   void CreateForTesting(ResourceId id);
352
353   GLenum TargetForTesting(ResourceId id);
354
355   // Sets the current read fence. If a resource is locked for read
356   // and has read fences enabled, the resource will not allow writes
357   // until this fence has passed.
358   void SetReadLockFence(Fence* fence) { current_read_lock_fence_ = fence; }
359
360   // Enable read lock fences for a specific resource.
361   void EnableReadLockFences(ResourceProvider::ResourceId id);
362
363   // Indicates if we can currently lock this resource for write.
364   bool CanLockForWrite(ResourceId id);
365
366   // Copy pixels from source to destination.
367   void CopyResource(ResourceId source_id, ResourceId dest_id);
368
369   static GLint GetActiveTextureUnit(gpu::gles2::GLES2Interface* gl);
370
371  private:
372   class GpuRasterBuffer;
373   class ImageRasterBuffer;
374   class PixelRasterBuffer;
375
376   struct Resource {
377     enum Origin { Internal, External, Delegated };
378
379     Resource();
380     ~Resource();
381     Resource(unsigned texture_id,
382              const gfx::Size& size,
383              Origin origin,
384              GLenum target,
385              GLenum filter,
386              GLenum texture_pool,
387              GLint wrap_mode,
388              TextureUsageHint hint,
389              ResourceFormat format);
390     Resource(uint8_t* pixels,
391              SharedBitmap* bitmap,
392              const gfx::Size& size,
393              Origin origin,
394              GLenum filter,
395              GLint wrap_mode);
396     Resource(const SharedBitmapId& bitmap_id,
397              const gfx::Size& size,
398              Origin origin,
399              GLenum filter,
400              GLint wrap_mode);
401
402     int child_id;
403     unsigned gl_id;
404     // Pixel buffer used for set pixels without unnecessary copying.
405     unsigned gl_pixel_buffer_id;
406     // Query used to determine when asynchronous set pixels complete.
407     unsigned gl_upload_query_id;
408     // Query used to determine when read lock fence has passed.
409     unsigned gl_read_lock_query_id;
410     TextureMailbox mailbox;
411     ReleaseCallback release_callback;
412     uint8_t* pixels;
413     int lock_for_read_count;
414     int imported_count;
415     int exported_count;
416     bool dirty_image : 1;
417     bool locked_for_write : 1;
418     bool lost : 1;
419     bool marked_for_deletion : 1;
420     bool pending_set_pixels : 1;
421     bool set_pixels_completion_forced : 1;
422     bool allocated : 1;
423     bool read_lock_fences_enabled : 1;
424     bool has_shared_bitmap_id : 1;
425     bool allow_overlay : 1;
426     scoped_refptr<Fence> read_lock_fence;
427     gfx::Size size;
428     Origin origin;
429     GLenum target;
430     // TODO(skyostil): Use a separate sampler object for filter state.
431     GLenum original_filter;
432     GLenum filter;
433     unsigned image_id;
434     unsigned bound_image_id;
435     GLenum texture_pool;
436     GLint wrap_mode;
437     TextureUsageHint hint;
438     ResourceType type;
439     ResourceFormat format;
440     SharedBitmapId shared_bitmap_id;
441     SharedBitmap* shared_bitmap;
442     linked_ptr<GpuRasterBuffer> gpu_raster_buffer;
443     linked_ptr<ImageRasterBuffer> image_raster_buffer;
444     linked_ptr<PixelRasterBuffer> pixel_raster_buffer;
445   };
446   typedef base::hash_map<ResourceId, Resource> ResourceMap;
447
448   class RasterBuffer {
449    public:
450     virtual ~RasterBuffer();
451
452     SkCanvas* LockForWrite();
453     // Returns true if canvas was written to while locked.
454     bool UnlockForWrite();
455
456    protected:
457     RasterBuffer(const Resource* resource, ResourceProvider* resource_provider);
458     const Resource* resource() const { return resource_; }
459     ResourceProvider* resource_provider() const { return resource_provider_; }
460
461     virtual SkCanvas* DoLockForWrite() = 0;
462     virtual bool DoUnlockForWrite() = 0;
463
464    private:
465     const Resource* resource_;
466     ResourceProvider* resource_provider_;
467     SkCanvas* locked_canvas_;
468     int canvas_save_count_;
469   };
470
471   class GpuRasterBuffer : public RasterBuffer {
472    public:
473     GpuRasterBuffer(const Resource* resource,
474                     ResourceProvider* resource_provider,
475                     bool use_distance_field_text);
476     virtual ~GpuRasterBuffer();
477
478    protected:
479     virtual SkCanvas* DoLockForWrite() OVERRIDE;
480     virtual bool DoUnlockForWrite() OVERRIDE;
481     skia::RefPtr<SkSurface> CreateSurface();
482
483    private:
484     skia::RefPtr<SkSurface> surface_;
485     uint32_t surface_generation_id_;
486     const bool use_distance_field_text_;
487
488     DISALLOW_COPY_AND_ASSIGN(GpuRasterBuffer);
489   };
490
491   class BitmapRasterBuffer : public RasterBuffer {
492    public:
493     virtual ~BitmapRasterBuffer();
494
495    protected:
496     BitmapRasterBuffer(const Resource* resource,
497                        ResourceProvider* resource_provider);
498
499     virtual SkCanvas* DoLockForWrite() OVERRIDE;
500     virtual bool DoUnlockForWrite() OVERRIDE;
501
502     virtual uint8_t* MapBuffer(int* stride) = 0;
503     virtual void UnmapBuffer() = 0;
504
505    private:
506     uint8_t* mapped_buffer_;
507     SkBitmap raster_bitmap_;
508     uint32_t raster_bitmap_generation_id_;
509     skia::RefPtr<SkCanvas> raster_canvas_;
510   };
511
512   class ImageRasterBuffer : public BitmapRasterBuffer {
513    public:
514     ImageRasterBuffer(const Resource* resource,
515                       ResourceProvider* resource_provider);
516     virtual ~ImageRasterBuffer();
517
518    protected:
519     virtual uint8_t* MapBuffer(int* stride) OVERRIDE;
520     virtual void UnmapBuffer() OVERRIDE;
521
522    private:
523     DISALLOW_COPY_AND_ASSIGN(ImageRasterBuffer);
524   };
525
526   class PixelRasterBuffer : public BitmapRasterBuffer {
527    public:
528     PixelRasterBuffer(const Resource* resource,
529                       ResourceProvider* resource_provider);
530     virtual ~PixelRasterBuffer();
531
532    protected:
533     virtual uint8_t* MapBuffer(int* stride) OVERRIDE;
534     virtual void UnmapBuffer() OVERRIDE;
535
536    private:
537     DISALLOW_COPY_AND_ASSIGN(PixelRasterBuffer);
538   };
539
540   static bool CompareResourceMapIteratorsByChildId(
541       const std::pair<ReturnedResource, ResourceMap::iterator>& a,
542       const std::pair<ReturnedResource, ResourceMap::iterator>& b);
543
544   struct Child {
545     Child();
546     ~Child();
547
548     ResourceIdMap child_to_parent_map;
549     ResourceIdMap parent_to_child_map;
550     ReturnCallback return_callback;
551     ResourceIdSet in_use_resources;
552     bool marked_for_deletion;
553   };
554   typedef base::hash_map<int, Child> ChildMap;
555
556   bool ReadLockFenceHasPassed(const Resource* resource) {
557     return !resource->read_lock_fence.get() ||
558            resource->read_lock_fence->HasPassed();
559   }
560
561   ResourceProvider(OutputSurface* output_surface,
562                    SharedBitmapManager* shared_bitmap_manager,
563                    int highp_threshold_min,
564                    bool use_rgba_4444_texture_format,
565                    size_t id_allocation_chunk_size,
566                    bool use_distance_field_text);
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   bool use_sync_query_;
651
652   bool use_distance_field_text_;
653
654   DISALLOW_COPY_AND_ASSIGN(ResourceProvider);
655 };
656
657
658 // TODO(epenner): Move these format conversions to resource_format.h
659 // once that builds on mac (npapi.h currently #includes OpenGL.h).
660 inline unsigned BitsPerPixel(ResourceFormat format) {
661   DCHECK_LE(format, RESOURCE_FORMAT_MAX);
662   static const unsigned format_bits_per_pixel[RESOURCE_FORMAT_MAX + 1] = {
663     32,  // RGBA_8888
664     16,  // RGBA_4444
665     32,  // BGRA_8888
666     8,   // ALPHA_8
667     8,   // LUMINANCE_8
668     16,  // RGB_565,
669     4    // ETC1
670   };
671   return format_bits_per_pixel[format];
672 }
673
674 inline GLenum GLDataType(ResourceFormat format) {
675   DCHECK_LE(format, RESOURCE_FORMAT_MAX);
676   static const unsigned format_gl_data_type[RESOURCE_FORMAT_MAX + 1] = {
677     GL_UNSIGNED_BYTE,           // RGBA_8888
678     GL_UNSIGNED_SHORT_4_4_4_4,  // RGBA_4444
679     GL_UNSIGNED_BYTE,           // BGRA_8888
680     GL_UNSIGNED_BYTE,           // ALPHA_8
681     GL_UNSIGNED_BYTE,           // LUMINANCE_8
682     GL_UNSIGNED_SHORT_5_6_5,    // RGB_565,
683     GL_UNSIGNED_BYTE            // ETC1
684   };
685   return format_gl_data_type[format];
686 }
687
688 inline GLenum GLDataFormat(ResourceFormat format) {
689   DCHECK_LE(format, RESOURCE_FORMAT_MAX);
690   static const unsigned format_gl_data_format[RESOURCE_FORMAT_MAX + 1] = {
691     GL_RGBA,           // RGBA_8888
692     GL_RGBA,           // RGBA_4444
693     GL_BGRA_EXT,       // BGRA_8888
694     GL_ALPHA,          // ALPHA_8
695     GL_LUMINANCE,      // LUMINANCE_8
696     GL_RGB,            // RGB_565
697     GL_ETC1_RGB8_OES   // ETC1
698   };
699   return format_gl_data_format[format];
700 }
701
702 inline GLenum GLInternalFormat(ResourceFormat format) {
703   return GLDataFormat(format);
704 }
705
706 }  // namespace cc
707
708 #endif  // CC_RESOURCES_RESOURCE_PROVIDER_H_