<commit_before>// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gvr

import (
	"context"
	"reflect"

	"github.com/google/gapid/gapis/api"
	"github.com/google/gapid/gapis/api/gles"
	"github.com/google/gapid/gapis/extensions"
	"github.com/google/gapid/gapis/resolve"
	"github.com/google/gapid/gapis/resolve/cmdgrouper"
	"github.com/google/gapid/gapis/service"
	"github.com/google/gapid/gapis/service/path"
)

func init() {
	extensions.Register(extensions.Extension{
		Name:           "GVR",
		AdjustContexts: adjustContexts,
		CmdGroupers:    newReprojectionGroupers,
		Events:         newReprojectionEvents,
	})
}

type contextUsage int

const (
	rendererCtx = contextUsage(iota)
	reprojectionCtx
)

func adjustContexts(ctx context.Context, ctxs []*api.ContextInfo) {
	// Look for the renderer context.
	tyGvrFrameSubmit := reflect.TypeOf(&Gvr_frame_submit{})
	if c := findContextByCommand(ctxs, tyGvrFrameSubmit); c != nil {
		c.UserData[rendererCtx] = true
		c.Name = "Main context (" + c.Name + ")"
	}

	// Look for the reprojection context.
	tyGlFlush := reflect.TypeOf(&gles.GlFlush{})
	if c := findContextByCommand(ctxs, tyGlFlush); c != nil {
		c.UserData[reprojectionCtx] = true
		c.Name = "Reprojection context"
	}
}

func findContextByCommand(ctxs []*api.ContextInfo, ty reflect.Type) *api.ContextInfo {
	highest, best := 0, (*api.ContextInfo)(nil)
	for _, c := range ctxs {
		if count := c.NumCommandsByType[ty]; count > highest {
			highest, best = count, c
		}
	}
	return best
}

func isReprojectionContext(ctx context.Context, p *path.Context) bool {
	// Only group if we're looking at the reprojection thread.
	if p == nil {
		return false
	}
	c, err := resolve.Context(ctx, p)
	if c == nil || err != nil {
		return false
	}
	_, ok := c.UserData[reprojectionCtx]
	return ok
}
<commit_msg>gvr: Minor tweak to GVR extension.<commit_after>// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gvr

import (
	"context"
	"reflect"

	"github.com/google/gapid/gapis/api"
	"github.com/google/gapid/gapis/api/gles"
	"github.com/google/gapid/gapis/extensions"
	"github.com/google/gapid/gapis/resolve"
	"github.com/google/gapid/gapis/resolve/cmdgrouper"
	"github.com/google/gapid/gapis/service"
	"github.com/google/gapid/gapis/service/path"
)

func init() {
	extensions.Register(extensions.Extension{
		Name:           "GVR",
		AdjustContexts: adjustContexts,
		CmdGroupers:    newReprojectionGroupers,
		Events:         newReprojectionEvents,
	})
}

type contextUsage int

const (
	rendererCtx = contextUsage(iota)
	reprojectionCtx
)

func adjustContexts(ctx context.Context, ctxs []*api.ContextInfo) {
	// Look for the renderer context.
	tyGvrFrameSubmit := reflect.TypeOf(&Gvr_frame_submit{})
	tyGlFlush := reflect.TypeOf(&gles.GlFlush{})

	renderer := findContextByCommand(ctxs, tyGvrFrameSubmit)
	reprojection := findContextByCommand(ctxs, tyGlFlush)

	if renderer != nil && reprojection != nil {
		renderer.UserData[rendererCtx] = true
		renderer.Name = "Main context (" + renderer.Name + ")"
		reprojection.UserData[reprojectionCtx] = true
		reprojection.Name = "Reprojection context (" + reprojection.Name + ")"
	}
}

func findContextByCommand(ctxs []*api.ContextInfo, ty reflect.Type) *api.ContextInfo {
	highest, best := 0, (*api.ContextInfo)(nil)
	for _, c := range ctxs {
		if count := c.NumCommandsByType[ty]; count > highest {
			highest, best = count, c
		}
	}
	return best
}

func isReprojectionContext(ctx context.Context, p *path.Context) bool {
	// Only group if we're looking at the reprojection thread.
	if p == nil {
		return false
	}
	c, err := resolve.Context(ctx, p)
	if c == nil || err != nil {
		return false
	}
	_, ok := c.UserData[reprojectionCtx]
	return ok
}

{'fraction_non_alphanumeric': 0.12223612849395077, 'fraction_numerical': 0.0037546933667083854, 'mean_word_length': 3.9129098360655736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3919438', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1588, 'n_words': 432}