Turn on GL drawing, allow switching via --gpu flag.

This CL comes after https://codereview.chromium.org/121303004/.

BUG=
R=robertphillips@google.com

Author: jcgregorio@google.com

Review URL: https://codereview.chromium.org/122453003

git-svn-id: http://skia.googlecode.com/svn/trunk@12923 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2014-01-06 20:03:55 +00:00
parent 31fdb92995
commit 9ee0e32886
2 changed files with 94 additions and 3 deletions

View File

@ -18,6 +18,8 @@ using namespace v8;
#include "gl/GrGLUtil.h"
#include "gl/GrGLDefines.h"
#include "gl/GrGLInterface.h"
#include "GrRenderTarget.h"
#include "GrContext.h"
#include "SkApplication.h"
#include "SkCommandLineFlags.h"
#include "SkData.h"
@ -28,6 +30,7 @@ using namespace v8;
DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n");
DEFINE_bool(gpu, true, "Use the GPU for rendering.");
void application_init() {
SkGraphics::Init();
@ -39,14 +42,78 @@ void application_term() {
SkGraphics::Term();
}
SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context)
: INHERITED(hwnd)
, fJsContext(context)
#if SK_SUPPORT_GPU
, fCurContext(NULL)
, fCurIntf(NULL)
, fCurRenderTarget(NULL)
#endif
{
this->setConfig(SkBitmap::kARGB_8888_Config);
this->setVisibleP(true);
this->setClipToBounds(false);
#if SK_SUPPORT_GPU
this->windowSizeChanged();
#endif
}
#if SK_SUPPORT_GPU
void SkV8ExampleWindow::windowSizeChanged() {
if (FLAGS_gpu) {
SkOSWindow::AttachmentInfo attachmentInfo;
bool result = this->attach(
SkOSWindow::kNativeGL_BackEndType, 0, &attachmentInfo);
if (!result) {
printf("Failed to attach.");
exit(1);
}
fCurIntf = GrGLCreateNativeInterface();
fCurContext = GrContext::Create(
kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
if (NULL == fCurIntf || NULL == fCurContext) {
printf("Failed to initialize GL.");
exit(1);
}
GrBackendRenderTargetDesc desc;
desc.fWidth = SkScalarRoundToInt(this->width());
desc.fHeight = SkScalarRoundToInt(this->height());
desc.fConfig = kSkia8888_GrPixelConfig;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc.fSampleCnt = attachmentInfo.fSampleCount;
desc.fStencilBits = attachmentInfo.fStencilBits;
GrGLint buffer;
GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
desc.fRenderTargetHandle = buffer;
SkSafeUnref(fCurRenderTarget);
fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
}
}
#endif
#if SK_SUPPORT_GPU
SkCanvas* SkV8ExampleWindow::createCanvas() {
if (FLAGS_gpu) {
SkAutoTUnref<SkBaseDevice> device(
new SkGpuDevice(fCurContext, fCurRenderTarget));
return new SkCanvas(device);
} else {
return this->INHERITED::createCanvas();
}
}
#endif
void SkV8ExampleWindow::onSizeChange() {
this->INHERITED::onSizeChange();
#if SK_SUPPORT_GPU
this->windowSizeChanged();
#endif
}
void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
@ -59,9 +126,15 @@ void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
canvas->restore();
INHERITED::onDraw(canvas);
}
this->INHERITED::onDraw(canvas);
#if SK_SUPPORT_GPU
if (FLAGS_gpu) {
fCurContext->flush();
this->present();
}
#endif
}
#ifdef SK_BUILD_FOR_WIN
void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
@ -114,5 +187,6 @@ SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
}
SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
global->setWindow(win);
return win;
}

View File

@ -12,6 +12,10 @@
#include "SkWindow.h"
class GrContext;
class GrGLInterface;
class GrRenderTarget;
class JsContext;
class SkV8ExampleWindow : public SkOSWindow {
@ -20,14 +24,27 @@ public:
protected:
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE;
virtual void onSizeChange() SK_OVERRIDE;
#if SK_SUPPORT_GPU
virtual SkCanvas* createCanvas() SK_OVERRIDE;
#endif
#ifdef SK_BUILD_FOR_WIN
virtual void onHandleInval(const SkIRect&) SK_OVERRIDE;
#endif
void windowSizeChanged();
private:
typedef SkOSWindow INHERITED;
JsContext* fJsContext;
#if SK_SUPPORT_GPU
GrContext* fCurContext;
const GrGLInterface* fCurIntf;
GrRenderTarget* fCurRenderTarget;
#endif
};
#endif