Add tests for TranslateLanguageList
[chromium-blink-merge.git] / cc / resources / resource_provider.h
blobd5b57127c205db172299d6cc7048e747779e56a8
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.
5 #ifndef CC_RESOURCES_RESOURCE_PROVIDER_H_
6 #define CC_RESOURCES_RESOURCE_PROVIDER_H_
8 #include <deque>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
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/base/resource_id.h"
22 #include "cc/output/context_provider.h"
23 #include "cc/output/output_surface.h"
24 #include "cc/resources/release_callback_impl.h"
25 #include "cc/resources/resource_format.h"
26 #include "cc/resources/return_callback.h"
27 #include "cc/resources/shared_bitmap.h"
28 #include "cc/resources/single_release_callback_impl.h"
29 #include "cc/resources/texture_mailbox.h"
30 #include "cc/resources/transferable_resource.h"
31 #include "third_party/khronos/GLES2/gl2.h"
32 #include "third_party/khronos/GLES2/gl2ext.h"
33 #include "third_party/skia/include/core/SkBitmap.h"
34 #include "third_party/skia/include/core/SkCanvas.h"
35 #include "ui/gfx/geometry/size.h"
37 class GrContext;
39 namespace gpu {
40 class GpuMemoryBufferManager;
41 namespace gles {
42 class GLES2Interface;
46 namespace gfx {
47 class GpuMemoryBuffer;
48 class Rect;
49 class Vector2d;
52 namespace cc {
53 class BlockingTaskRunner;
54 class IdAllocator;
55 class SharedBitmap;
56 class SharedBitmapManager;
58 // This class is not thread-safe and can only be called from the thread it was
59 // created on (in practice, the impl thread).
60 class CC_EXPORT ResourceProvider {
61 private:
62 struct Resource;
64 public:
65 typedef std::vector<ResourceId> ResourceIdArray;
66 typedef base::hash_set<ResourceId> ResourceIdSet;
67 typedef base::hash_map<ResourceId, ResourceId> ResourceIdMap;
68 enum TextureHint {
69 TEXTURE_HINT_DEFAULT = 0x0,
70 TEXTURE_HINT_IMMUTABLE = 0x1,
71 TEXTURE_HINT_FRAMEBUFFER = 0x2,
72 TEXTURE_HINT_IMMUTABLE_FRAMEBUFFER =
73 TEXTURE_HINT_IMMUTABLE | TEXTURE_HINT_FRAMEBUFFER
75 enum ResourceType {
76 RESOURCE_TYPE_GL_TEXTURE,
77 RESOURCE_TYPE_BITMAP,
80 static scoped_ptr<ResourceProvider> Create(
81 OutputSurface* output_surface,
82 SharedBitmapManager* shared_bitmap_manager,
83 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
84 BlockingTaskRunner* blocking_main_thread_task_runner,
85 int highp_threshold_min,
86 bool use_rgba_4444_texture_format,
87 size_t id_allocation_chunk_size,
88 bool use_persistent_map_for_gpu_memory_buffers);
89 virtual ~ResourceProvider();
91 void DidLoseOutputSurface() { lost_output_surface_ = true; }
93 int max_texture_size() const { return max_texture_size_; }
94 ResourceFormat memory_efficient_texture_format() const {
95 return use_rgba_4444_texture_format_ ? RGBA_4444 : best_texture_format_;
97 ResourceFormat best_texture_format() const { return best_texture_format_; }
98 ResourceFormat best_render_buffer_format() const {
99 return best_render_buffer_format_;
101 ResourceFormat yuv_resource_format() const { return yuv_resource_format_; }
102 bool use_sync_query() const { return use_sync_query_; }
103 bool use_persistent_map_for_gpu_memory_buffers() const {
104 return use_persistent_map_for_gpu_memory_buffers_;
106 size_t num_resources() const { return resources_.size(); }
108 // Checks whether a resource is in use by a consumer.
109 bool InUseByConsumer(ResourceId id);
111 bool IsLost(ResourceId id);
113 void EnableReadLockFencesForTesting(ResourceId id);
115 // Producer interface.
117 ResourceType default_resource_type() const { return default_resource_type_; }
118 ResourceType GetResourceType(ResourceId id);
120 // Creates a resource of the default resource type.
121 ResourceId CreateResource(const gfx::Size& size,
122 GLint wrap_mode,
123 TextureHint hint,
124 ResourceFormat format);
126 // Creates a resource which is tagged as being managed for GPU memory
127 // accounting purposes.
128 ResourceId CreateManagedResource(const gfx::Size& size,
129 GLenum target,
130 GLint wrap_mode,
131 TextureHint hint,
132 ResourceFormat format);
134 // You can also explicitly create a specific resource type.
135 ResourceId CreateGLTexture(const gfx::Size& size,
136 GLenum target,
137 GLenum texture_pool,
138 GLint wrap_mode,
139 TextureHint hint,
140 ResourceFormat format);
142 ResourceId CreateBitmap(const gfx::Size& size, GLint wrap_mode);
143 // Wraps an IOSurface into a GL resource.
144 ResourceId CreateResourceFromIOSurface(const gfx::Size& size,
145 unsigned io_surface_id);
147 // Wraps an external texture mailbox into a GL resource.
148 ResourceId CreateResourceFromTextureMailbox(
149 const TextureMailbox& mailbox,
150 scoped_ptr<SingleReleaseCallbackImpl> release_callback_impl);
152 ResourceId CreateResourceFromTextureMailbox(
153 const TextureMailbox& mailbox,
154 scoped_ptr<SingleReleaseCallbackImpl> release_callback_impl,
155 bool read_lock_fences_enabled);
157 void DeleteResource(ResourceId id);
159 // Update pixels from image, copying source_rect (in image) to dest_offset (in
160 // the resource).
161 void CopyToResource(ResourceId id,
162 const uint8_t* image,
163 const gfx::Size& image_size);
165 // Only flush the command buffer if supported.
166 // Returns true if the shallow flush occurred, false otherwise.
167 bool ShallowFlushIfSupported();
169 // Creates accounting for a child. Returns a child ID.
170 int CreateChild(const ReturnCallback& return_callback);
172 // Destroys accounting for the child, deleting all accounted resources.
173 void DestroyChild(int child);
175 // Sets whether resources need sync points set on them when returned to this
176 // child. Defaults to true.
177 void SetChildNeedsSyncPoints(int child, bool needs_sync_points);
179 // Gets the child->parent resource ID map.
180 const ResourceIdMap& GetChildToParentMap(int child) const;
182 // Prepares resources to be transfered to the parent, moving them to
183 // mailboxes and serializing meta-data into TransferableResources.
184 // Resources are not removed from the ResourceProvider, but are marked as
185 // "in use".
186 void PrepareSendToParent(const ResourceIdArray& resources,
187 TransferableResourceArray* transferable_resources);
189 // Receives resources from a child, moving them from mailboxes. Resource IDs
190 // passed are in the child namespace, and will be translated to the parent
191 // namespace, added to the child->parent map.
192 // This adds the resources to the working set in the ResourceProvider without
193 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
194 // after calling this method to do that. All calls to ReceiveFromChild should
195 // be followed by a DeclareUsedResourcesFromChild.
196 // NOTE: if the sync_point is set on any TransferableResource, this will
197 // wait on it.
198 void ReceiveFromChild(
199 int child, const TransferableResourceArray& transferable_resources);
201 // Once a set of resources have been received, they may or may not be used.
202 // This declares what set of resources are currently in use from the child,
203 // releasing any other resources back to the child.
204 void DeclareUsedResourcesFromChild(int child,
205 const ResourceIdSet& resources_from_child);
207 // Receives resources from the parent, moving them from mailboxes. Resource
208 // IDs passed are in the child namespace.
209 // NOTE: if the sync_point is set on any TransferableResource, this will
210 // wait on it.
211 void ReceiveReturnsFromParent(
212 const ReturnedResourceArray& transferable_resources);
214 // The following lock classes are part of the ResourceProvider API and are
215 // needed to read and write the resource contents. The user must ensure
216 // that they only use GL locks on GL resources, etc, and this is enforced
217 // by assertions.
218 class CC_EXPORT ScopedReadLockGL {
219 public:
220 ScopedReadLockGL(ResourceProvider* resource_provider,
221 ResourceId resource_id);
222 virtual ~ScopedReadLockGL();
224 unsigned texture_id() const { return resource_->gl_id; }
225 GLenum target() const { return resource_->target; }
227 protected:
228 ResourceProvider* resource_provider_;
229 ResourceId resource_id_;
231 private:
232 const ResourceProvider::Resource* resource_;
234 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL);
237 class CC_EXPORT ScopedSamplerGL : public ScopedReadLockGL {
238 public:
239 ScopedSamplerGL(ResourceProvider* resource_provider,
240 ResourceId resource_id,
241 GLenum filter);
242 ScopedSamplerGL(ResourceProvider* resource_provider,
243 ResourceId resource_id,
244 GLenum unit,
245 GLenum filter);
246 ~ScopedSamplerGL() override;
248 GLenum target() const { return target_; }
250 private:
251 GLenum unit_;
252 GLenum target_;
254 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL);
257 class CC_EXPORT ScopedWriteLockGL {
258 public:
259 ScopedWriteLockGL(ResourceProvider* resource_provider,
260 ResourceId resource_id);
261 ~ScopedWriteLockGL();
263 unsigned texture_id() const { return texture_id_; }
265 private:
266 ResourceProvider* resource_provider_;
267 ResourceProvider::Resource* resource_;
268 unsigned texture_id_;
270 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL);
273 class CC_EXPORT ScopedReadLockSoftware {
274 public:
275 ScopedReadLockSoftware(ResourceProvider* resource_provider,
276 ResourceId resource_id);
277 ~ScopedReadLockSoftware();
279 const SkBitmap* sk_bitmap() const {
280 DCHECK(valid());
281 return &sk_bitmap_;
283 GLint wrap_mode() const { return wrap_mode_; }
285 bool valid() const { return !!sk_bitmap_.getPixels(); }
287 private:
288 ResourceProvider* resource_provider_;
289 ResourceId resource_id_;
290 SkBitmap sk_bitmap_;
291 GLint wrap_mode_;
293 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware);
296 class CC_EXPORT ScopedWriteLockSoftware {
297 public:
298 ScopedWriteLockSoftware(ResourceProvider* resource_provider,
299 ResourceId resource_id);
300 ~ScopedWriteLockSoftware();
302 SkBitmap& sk_bitmap() { return sk_bitmap_; }
303 bool valid() const { return !!sk_bitmap_.getPixels(); }
305 private:
306 ResourceProvider* resource_provider_;
307 ResourceProvider::Resource* resource_;
308 SkBitmap sk_bitmap_;
309 base::ThreadChecker thread_checker_;
311 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware);
314 class CC_EXPORT ScopedWriteLockGpuMemoryBuffer {
315 public:
316 ScopedWriteLockGpuMemoryBuffer(ResourceProvider* resource_provider,
317 ResourceId resource_id);
318 ~ScopedWriteLockGpuMemoryBuffer();
320 gfx::GpuMemoryBuffer* GetGpuMemoryBuffer();
322 private:
323 ResourceProvider* resource_provider_;
324 ResourceProvider::Resource* resource_;
325 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
326 gfx::GpuMemoryBuffer* gpu_memory_buffer_;
327 gfx::Size size_;
328 ResourceFormat format_;
329 base::ThreadChecker thread_checker_;
331 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGpuMemoryBuffer);
334 class CC_EXPORT ScopedWriteLockGr {
335 public:
336 ScopedWriteLockGr(ResourceProvider* resource_provider,
337 ResourceId resource_id);
338 ~ScopedWriteLockGr();
340 void InitSkSurface(bool use_distance_field_text,
341 bool can_use_lcd_text,
342 int msaa_sample_count);
343 void ReleaseSkSurface();
345 SkSurface* sk_surface() { return sk_surface_.get(); }
346 ResourceProvider::Resource* resource() { return resource_; }
348 private:
349 ResourceProvider* resource_provider_;
350 ResourceProvider::Resource* resource_;
351 base::ThreadChecker thread_checker_;
352 skia::RefPtr<SkSurface> sk_surface_;
354 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGr);
357 class Fence : public base::RefCounted<Fence> {
358 public:
359 Fence() {}
361 virtual void Set() = 0;
362 virtual bool HasPassed() = 0;
363 virtual void Wait() = 0;
365 protected:
366 friend class base::RefCounted<Fence>;
367 virtual ~Fence() {}
369 private:
370 DISALLOW_COPY_AND_ASSIGN(Fence);
373 class SynchronousFence : public ResourceProvider::Fence {
374 public:
375 explicit SynchronousFence(gpu::gles2::GLES2Interface* gl);
377 // Overridden from Fence:
378 void Set() override;
379 bool HasPassed() override;
380 void Wait() override;
382 // Returns true if fence has been set but not yet synchornized.
383 bool has_synchronized() const { return has_synchronized_; }
385 private:
386 ~SynchronousFence() override;
388 void Synchronize();
390 gpu::gles2::GLES2Interface* gl_;
391 bool has_synchronized_;
393 DISALLOW_COPY_AND_ASSIGN(SynchronousFence);
396 // Acquire pixel buffer for resource. The pixel buffer can be used to
397 // set resource pixels without performing unnecessary copying.
398 void AcquirePixelBuffer(ResourceId resource);
399 void ReleasePixelBuffer(ResourceId resource);
400 // Map/unmap the acquired pixel buffer.
401 uint8_t* MapPixelBuffer(ResourceId id, int* stride);
402 void UnmapPixelBuffer(ResourceId id);
403 // Asynchronously update pixels from acquired pixel buffer.
404 void BeginSetPixels(ResourceId id);
405 void ForceSetPixelsToComplete(ResourceId id);
406 bool DidSetPixelsComplete(ResourceId id);
408 // For tests only! This prevents detecting uninitialized reads.
409 // Use SetPixels or LockForWrite to allocate implicitly.
410 void AllocateForTesting(ResourceId id);
412 // For tests only!
413 void CreateForTesting(ResourceId id);
415 GLenum TargetForTesting(ResourceId id);
417 // Sets the current read fence. If a resource is locked for read
418 // and has read fences enabled, the resource will not allow writes
419 // until this fence has passed.
420 void SetReadLockFence(Fence* fence) { current_read_lock_fence_ = fence; }
422 // Indicates if we can currently lock this resource for write.
423 bool CanLockForWrite(ResourceId id);
425 // Copy |rect| pixels from source to destination.
426 void CopyResource(ResourceId source_id,
427 ResourceId dest_id,
428 const gfx::Rect& rect);
430 void WaitSyncPointIfNeeded(ResourceId id);
432 void WaitReadLockIfNeeded(ResourceId id);
434 static GLint GetActiveTextureUnit(gpu::gles2::GLES2Interface* gl);
436 OutputSurface* output_surface() { return output_surface_; }
438 void ValidateResource(ResourceId id) const;
440 protected:
441 ResourceProvider(OutputSurface* output_surface,
442 SharedBitmapManager* shared_bitmap_manager,
443 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
444 BlockingTaskRunner* blocking_main_thread_task_runner,
445 int highp_threshold_min,
446 bool use_rgba_4444_texture_format,
447 size_t id_allocation_chunk_size,
448 bool use_persistent_map_for_gpu_memory_buffers);
449 void Initialize();
451 private:
452 struct Resource {
453 enum Origin { INTERNAL, EXTERNAL, DELEGATED };
455 ~Resource();
456 Resource(unsigned texture_id,
457 const gfx::Size& size,
458 Origin origin,
459 GLenum target,
460 GLenum filter,
461 GLenum texture_pool,
462 GLint wrap_mode,
463 TextureHint hint,
464 ResourceFormat format);
465 Resource(uint8_t* pixels,
466 SharedBitmap* bitmap,
467 const gfx::Size& size,
468 Origin origin,
469 GLenum filter,
470 GLint wrap_mode);
471 Resource(const SharedBitmapId& bitmap_id,
472 const gfx::Size& size,
473 Origin origin,
474 GLenum filter,
475 GLint wrap_mode);
477 int child_id;
478 unsigned gl_id;
479 // Pixel buffer used for set pixels without unnecessary copying.
480 unsigned gl_pixel_buffer_id;
481 // Query used to determine when asynchronous set pixels complete.
482 unsigned gl_upload_query_id;
483 // Query used to determine when read lock fence has passed.
484 unsigned gl_read_lock_query_id;
485 TextureMailbox mailbox;
486 ReleaseCallbackImpl release_callback_impl;
487 uint8_t* pixels;
488 int lock_for_read_count;
489 int imported_count;
490 int exported_count;
491 bool dirty_image : 1;
492 bool locked_for_write : 1;
493 bool lost : 1;
494 bool marked_for_deletion : 1;
495 bool pending_set_pixels : 1;
496 bool set_pixels_completion_forced : 1;
497 bool allocated : 1;
498 bool read_lock_fences_enabled : 1;
499 bool has_shared_bitmap_id : 1;
500 scoped_refptr<Fence> read_lock_fence;
501 gfx::Size size;
502 Origin origin;
503 GLenum target;
504 // TODO(skyostil): Use a separate sampler object for filter state.
505 GLenum original_filter;
506 GLenum filter;
507 unsigned image_id;
508 unsigned bound_image_id;
509 GLenum texture_pool;
510 GLint wrap_mode;
511 TextureHint hint;
512 ResourceType type;
513 ResourceFormat format;
514 SharedBitmapId shared_bitmap_id;
515 SharedBitmap* shared_bitmap;
516 gfx::GpuMemoryBuffer* gpu_memory_buffer;
518 typedef base::hash_map<ResourceId, Resource> ResourceMap;
520 struct Child {
521 Child();
522 ~Child();
524 ResourceIdMap child_to_parent_map;
525 ResourceIdMap parent_to_child_map;
526 ReturnCallback return_callback;
527 bool marked_for_deletion;
528 bool needs_sync_points;
530 typedef base::hash_map<int, Child> ChildMap;
532 bool ReadLockFenceHasPassed(const Resource* resource) {
533 return !resource->read_lock_fence.get() ||
534 resource->read_lock_fence->HasPassed();
537 Resource* InsertResource(ResourceId id, const Resource& resource);
538 Resource* GetResource(ResourceId id);
539 const Resource* LockForRead(ResourceId id);
540 void UnlockForRead(ResourceId id);
541 Resource* LockForWrite(ResourceId id);
542 void UnlockForWrite(Resource* resource);
544 static void PopulateSkBitmapWithResource(SkBitmap* sk_bitmap,
545 const Resource* resource);
547 void TransferResource(gpu::gles2::GLES2Interface* gl,
548 ResourceId id,
549 TransferableResource* resource);
550 enum DeleteStyle {
551 NORMAL,
552 FOR_SHUTDOWN,
554 void DeleteResourceInternal(ResourceMap::iterator it, DeleteStyle style);
555 void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it,
556 DeleteStyle style,
557 const ResourceIdArray& unused);
558 void DestroyChildInternal(ChildMap::iterator it, DeleteStyle style);
559 void LazyCreate(Resource* resource);
560 void LazyAllocate(Resource* resource);
562 void BindImageForSampling(Resource* resource);
563 // Binds the given GL resource to a texture target for sampling using the
564 // specified filter for both minification and magnification. Returns the
565 // texture target used. The resource must be locked for reading.
566 GLenum BindForSampling(ResourceId resource_id, GLenum unit, GLenum filter);
568 // Returns NULL if the output_surface_ does not have a ContextProvider.
569 gpu::gles2::GLES2Interface* ContextGL() const;
570 class GrContext* GrContext(bool worker_context) const;
572 OutputSurface* output_surface_;
573 SharedBitmapManager* shared_bitmap_manager_;
574 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
575 BlockingTaskRunner* blocking_main_thread_task_runner_;
576 bool lost_output_surface_;
577 int highp_threshold_min_;
578 ResourceId next_id_;
579 ResourceMap resources_;
580 int next_child_;
581 ChildMap children_;
583 ResourceType default_resource_type_;
584 bool use_texture_storage_ext_;
585 bool use_texture_format_bgra_;
586 bool use_texture_usage_hint_;
587 bool use_compressed_texture_etc1_;
588 ResourceFormat yuv_resource_format_;
589 int max_texture_size_;
590 ResourceFormat best_texture_format_;
591 ResourceFormat best_render_buffer_format_;
593 base::ThreadChecker thread_checker_;
595 scoped_refptr<Fence> current_read_lock_fence_;
596 bool use_rgba_4444_texture_format_;
598 const size_t id_allocation_chunk_size_;
599 scoped_ptr<IdAllocator> texture_id_allocator_;
600 scoped_ptr<IdAllocator> buffer_id_allocator_;
602 bool use_sync_query_;
603 bool use_persistent_map_for_gpu_memory_buffers_;
604 // Fence used for CopyResource if CHROMIUM_sync_query is not supported.
605 scoped_refptr<SynchronousFence> synchronous_fence_;
607 DISALLOW_COPY_AND_ASSIGN(ResourceProvider);
610 // TODO(epenner): Move these format conversions to resource_format.h
611 // once that builds on mac (npapi.h currently #includes OpenGL.h).
612 inline int BitsPerPixel(ResourceFormat format) {
613 switch (format) {
614 case BGRA_8888:
615 case RGBA_8888:
616 return 32;
617 case RGBA_4444:
618 case RGB_565:
619 return 16;
620 case ALPHA_8:
621 case LUMINANCE_8:
622 case RED_8:
623 return 8;
624 case ETC1:
625 return 4;
627 NOTREACHED();
628 return 0;
631 inline GLenum GLDataType(ResourceFormat format) {
632 DCHECK_LE(format, RESOURCE_FORMAT_MAX);
633 static const unsigned format_gl_data_type[RESOURCE_FORMAT_MAX + 1] = {
634 GL_UNSIGNED_BYTE, // RGBA_8888
635 GL_UNSIGNED_SHORT_4_4_4_4, // RGBA_4444
636 GL_UNSIGNED_BYTE, // BGRA_8888
637 GL_UNSIGNED_BYTE, // ALPHA_8
638 GL_UNSIGNED_BYTE, // LUMINANCE_8
639 GL_UNSIGNED_SHORT_5_6_5, // RGB_565,
640 GL_UNSIGNED_BYTE, // ETC1
641 GL_UNSIGNED_BYTE // RED_8
643 return format_gl_data_type[format];
646 inline GLenum GLDataFormat(ResourceFormat format) {
647 DCHECK_LE(format, RESOURCE_FORMAT_MAX);
648 static const unsigned format_gl_data_format[RESOURCE_FORMAT_MAX + 1] = {
649 GL_RGBA, // RGBA_8888
650 GL_RGBA, // RGBA_4444
651 GL_BGRA_EXT, // BGRA_8888
652 GL_ALPHA, // ALPHA_8
653 GL_LUMINANCE, // LUMINANCE_8
654 GL_RGB, // RGB_565
655 GL_ETC1_RGB8_OES, // ETC1
656 GL_RED_EXT // RED_8
658 return format_gl_data_format[format];
661 inline GLenum GLInternalFormat(ResourceFormat format) {
662 return GLDataFormat(format);
665 } // namespace cc
667 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_