Skip to content

Commit

Permalink
Enable video painting on Canvas for Chrome on Android. This change sh…
Browse files Browse the repository at this point in the history
…ould land after another patch landing in https://codereview.chromium.org/13685002.

BUG=147265
TEST=visited the following site:
http://html5doctor.com/demos/video-canvas-magic/demo1.html

Visit http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_canvas_drawimage_video.
Change different parameters of the canvas and video. Compare the result between desktop chrome and android chrome to see if video painting results are the same.

Canvas 500X400 ctx.drawImage(v,60,60,320,176)                PASS
Canvas 500X400 ctx.drawImage(v,0,0,320,176)                  PASS
Canvas 200X100 ctx.drawImage(v,0,0,320,176)                  PASS
Canvas 200X100 ctx.drawImage(v,50,50,320,176)                PASS
Canvas 500X400 ctx.drawImage(v,0,0,30,30,5,5,260,125)        PASS
Canvas 500X400 ctx.drawImage(v,0,0)                          PASS
Canvas 600x700 ctx.drawImage(v,90,90,80,80,100,100,260,125)  PASS

TODO:
More complex and mixed 2D Canvas operation to see if the painting result is right(Need to learn some more JS for that).

Review URL: https://chromiumcodereview.appspot.com/13620008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@194785 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
hkuang@chromium.org committed Apr 18, 2013
1 parent 4f60d80 commit 5394a41
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 10 deletions.
30 changes: 29 additions & 1 deletion gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ struct ShaderInfo {
const ShaderInfo shader_infos[] = {
// VERTEX_SHADER_POS_TEX
SHADER(
uniform mat4 u_matrix;
attribute vec4 a_position;
varying vec2 v_uv;
void main(void) {
gl_Position = a_position;
gl_Position = u_matrix * a_position;
v_uv = a_position.xy * 0.5 + vec2(0.5, 0.5);
}),
// FRAGMENT_SHADER_TEX
Expand Down Expand Up @@ -279,6 +280,9 @@ void CopyTextureCHROMIUMResourceManager::Initialize(

sampler_locations_[program] = glGetUniformLocation(programs_[program],
"u_texSampler");

matrix_handle_[program] = glGetUniformLocation(programs_[program],
"u_matrix");
}

for (int shader = 0; shader < kNumShaders; ++shader)
Expand Down Expand Up @@ -313,6 +317,29 @@ void CopyTextureCHROMIUMResourceManager::DoCopyTexture(
bool flip_y,
bool premultiply_alpha,
bool unpremultiply_alpha) {
// Use default transform matrix if no transform passed in.
const static GLfloat default_matrix[16] = {1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
DoCopyTextureWithTransform(decoder, source_target, dest_target, source_id,
dest_id, level, width, height, flip_y, premultiply_alpha,
unpremultiply_alpha, default_matrix);
}

void CopyTextureCHROMIUMResourceManager::DoCopyTextureWithTransform(
const gles2::GLES2Decoder* decoder,
GLenum source_target,
GLenum dest_target,
GLuint source_id,
GLuint dest_id,
GLint level,
GLsizei width,
GLsizei height,
bool flip_y,
bool premultiply_alpha,
bool unpremultiply_alpha,
const GLfloat transform_matrix[16]) {
DCHECK(source_target == GL_TEXTURE_2D ||
source_target == GL_TEXTURE_EXTERNAL_OES);
if (!initialized_) {
Expand All @@ -335,6 +362,7 @@ void CopyTextureCHROMIUMResourceManager::DoCopyTexture(
}
#endif

glUniformMatrix4fv(matrix_handle_[program], 1, GL_FALSE, transform_matrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, dest_id);
// NVidia drivers require texture settings to be a certain way
Expand Down
11 changes: 11 additions & 0 deletions gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class GPU_EXPORT CopyTextureCHROMIUMResourceManager {
bool flip_y, bool premultiply_alpha,
bool unpremultiply_alpha);

// This will apply a transform on the source texture before copying to
// destination texture.
void DoCopyTextureWithTransform(const gles2::GLES2Decoder* decoder,
GLenum source_target, GLenum dest_target,
GLuint source_id, GLuint dest_id, GLint level,
GLsizei width, GLsizei height, bool flip_y,
bool premultiply_alpha,
bool unpremultiply_alpha,
const GLfloat transform_matrix[16]);

// The attributes used during invocation of the extension.
static const GLuint kVertexPositionAttrib = 0;

Expand All @@ -41,6 +51,7 @@ class GPU_EXPORT CopyTextureCHROMIUMResourceManager {
GLuint programs_[kNumPrograms];
GLuint buffer_id_;
GLuint framebuffer_;
GLuint matrix_handle_[kNumPrograms];
GLuint sampler_locations_[kNumPrograms];

DISALLOW_COPY_AND_ASSIGN(CopyTextureCHROMIUMResourceManager);
Expand Down
41 changes: 32 additions & 9 deletions gpu/command_buffer/service/gles2_cmd_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9852,15 +9852,38 @@ void GLES2DecoderImpl::DoCopyTextureCHROMIUM(
dest_texture, GL_TEXTURE_2D, level, true);
}

copy_texture_CHROMIUM_->DoCopyTexture(this,
source_texture->target(),
dest_texture->target(),
source_texture->service_id(),
dest_texture->service_id(), level,
source_width, source_height,
unpack_flip_y_,
unpack_premultiply_alpha_,
unpack_unpremultiply_alpha_);
// GL_TEXTURE_EXTERNAL_OES texture requires apply a transform matrix
// before presenting.
if (source_texture->target() == GL_TEXTURE_EXTERNAL_OES) {
// TODO(hkuang): get the StreamTexture transform matrix in GPU process
// instead of using default matrix crbug.com/226218.
const static GLfloat default_matrix[16] = {1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
copy_texture_CHROMIUM_->DoCopyTextureWithTransform(
this,
source_texture->target(),
dest_texture->target(),
source_texture->service_id(),
dest_texture->service_id(), level,
source_width, source_height,
unpack_flip_y_,
unpack_premultiply_alpha_,
unpack_unpremultiply_alpha_,
default_matrix);
} else {
copy_texture_CHROMIUM_->DoCopyTexture(
this,
source_texture->target(),
dest_texture->target(),
source_texture->service_id(),
dest_texture->service_id(), level,
source_width, source_height,
unpack_flip_y_,
unpack_premultiply_alpha_,
unpack_unpremultiply_alpha_);
}
}

static GLenum ExtractTypeFromStorageFormat(GLenum internalformat) {
Expand Down
29 changes: 29 additions & 0 deletions webkit/media/android/webmediaplayer_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "cc/layers/video_layer.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "media/base/android/media_player_bridge.h"
#include "media/base/video_frame.h"
#include "net/base/mime_util.h"
Expand Down Expand Up @@ -270,6 +271,34 @@ void WebMediaPlayerAndroid::paint(WebKit::WebCanvas* canvas,
NOTIMPLEMENTED();
}

bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture(
WebKit::WebGraphicsContext3D* web_graphics_context,
unsigned int texture,
unsigned int level,
unsigned int internal_format,
bool premultiply_alpha,
bool flip_y) {
if (!texture_id_)
return false;

// The video is stored in an unmultiplied format, so premultiply if
// necessary.
web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
premultiply_alpha);

// Application itself needs to take care of setting the right flip_y
// value down to get the expected result.
// flip_y==true means to reverse the video orientation while
// flip_y==false means to keep the intrinsic orientation.
web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y);
web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D, texture_id_,
texture, level, internal_format);
web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false);
web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
false);
return true;
}

bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const {
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions webkit/media/android/webmediaplayer_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/message_loop.h"
#include "base/time.h"
#include "cc/layers/video_frame_provider.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayer.h"
Expand Down Expand Up @@ -65,6 +66,14 @@ class WebMediaPlayerAndroid
const WebKit::WebRect& rect,
uint8_t alpha);

virtual bool copyVideoTextureToPlatformTexture(
WebKit::WebGraphicsContext3D* web_graphics_context,
unsigned int texture,
unsigned int level,
unsigned int internal_format,
bool premultiply_alpha,
bool flip_y);

// True if the loaded media has a playable video/audio track.
virtual bool hasVideo() const;
virtual bool hasAudio() const;
Expand Down

0 comments on commit 5394a41

Please sign in to comment.